2017-09-12 45 views
0

我試圖找到一個解決方案的一些問題(給定一個數組A [] n個數字和另一個數字x,確定是否存在A中的兩個元素之和確實是X) 這是我的解決方案:std :: set在g ++編譯後給出錯誤

#include <iostream> 
#include <vector> 
#include <set> 
#include <string> 

#include "stdio.h" 

using std::cout; 
using std::vector; 

bool hasPairWithSum(vector<int> data, int sum); 

int main() 
{ 
    int testCases; 
    std::cin >> testCases; 
    int arrSize; 
    int sum; 
    std::vector<std::string> results; 
    vector<int> vec; 

    for (int i = 0; i < testCases; i++) { 
     std::cin >> arrSize; 
     std::cin >> sum; 

     for (int j = 0; j < arrSize; j++) 
     { 
     int tmp; 
     std::cin >> tmp; 
     vec.push_back(tmp); 
     } 
     bool result = hasPairWithSum(vec, sum); 
     if (result) 
     results.push_back("YES"); 
     else results.push_back("NO"); 
    } 

    for (int k = 0; k < results.size(); k++) 
     cout << results[k]<< std::endl; 

    return 0; 
} 

bool hasPairWithSum(vector<int> data, int sum) { 
    std::set<int> compl; 
    for (int j = 0; j < data.size(); j++) { 
     int currentCompl = sum - data[j]; 
     if (compl.find(data[j]) != compl.end()) 
      return true; 
     compl.insert(currentCompl); 
    } 
    return false; 
} 

我使用C++。本地它工作正常,但與網站在線compilator(它使用G ++ 5.4),它提供了以下錯誤: prog.cpp: In function 'bool hasPairWithSum(std::vector, int)': prog.cpp:45:21: error: expected class-name before ';' token std::set compl; ^ prog.cpp:48:12: error: expected primary-expression before '.' token if (compl.find(data[j]) != compl.end()) ^ prog.cpp:48:35: error: expected primary-expression before '.' token if (compl.find(data[j]) != compl.end()) ^ prog.cpp:50:8: error: expected primary-expression before '.' token compl.insert(currentCompl); ^

任何人有任何想法,我怎麼能解決我的解決方案,使在編譯的G ++? 謝謝!

+0

'的std ::集併發症的東西;'需要一個模板參數,比如'的std ::設置併發症;'什麼的。編輯:雖然它似乎有一個在你的代碼?你點擊保存? – Charles

+1

錯誤信息與顯示的代碼不符。 –

+0

@SamVarshavchik當我在這裏運行它時:http://practice.geeksforgeeks.org/problems/key-pair/0 我得到這個錯誤。 – 1011sophie

回答

4

您正遇到拼寫操作員的鮮爲人知的替代方式。對於世界上某些鍵盤而言,某些特殊鍵很難鍵入,因此與具有相同含義(並且解析相同)的操作符的名稱與更普通的操作符相同。大多數代碼不使用它們。

這裏的備用令牌解析器知道名單:

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq 

併發症是另一種方式拼寫〜而且是按位恭維操作。

您的變量只是重命名爲別的