我試圖找到一個解決方案的一些問題(給定一個數組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 ++? 謝謝!
'的std ::集併發症的東西;'需要一個模板參數,比如'的std ::設置併發症;'什麼的。編輯:雖然它似乎有一個在你的代碼?你點擊保存? –
Charles
錯誤信息與顯示的代碼不符。 –
@SamVarshavchik當我在這裏運行它時:http://practice.geeksforgeeks.org/problems/key-pair/0 我得到這個錯誤。 – 1011sophie