STD :: Set在G 汇编后会出现错误
std::set is giving errors after g++ compilation
我正在尝试找到解决方案的解决方案(给定一个数字的数组a []和另一个数字x,确定一个总和中是否存在两个元素恰好x。)这是我的解决方案:
#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 。本地工作正常,但是在网站在线编译器(使用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 中可以编译?谢谢你!
问题是compl
是C 关键字。使用其他标识符。
您遇到了拼写运算符的鲜为人知的替代方法。对于世界各地的某些键盘,某些特殊键很难键入,因此对于操作员来说,具有与更普通运算符相同的含义(并且被解析相同)的姓名。大多数代码不使用它们。
这是解析器所知道的替代令牌的列表:
and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq
compl是拼写〜的另一种方式,是位的互补操作员。
只需将您的变量重命名为其他东西
- SSE 内联汇编和可能的 g++ 优化错误
- 汇编错误:调用基本型构造函数时无效的转换
- 为什么我的GLSL着色器失败汇编而没有错误消息
- 为什么将char*传递给字符串参数会产生汇编错误
- Android Studio 3 C 文件中充满了错误,但汇编还可以
- C 汇编错误:无匹配的构造函数以进行初始化
- C 11线程汇编错误传递字符串作为复制的引用
- Python安装汇编错误
- binary_search on属性的汇编错误
- 从源构建时,提升iostreams中的汇编错误
- Gmock方法正在抛出汇编错误
- G 生成逻辑上错误的汇编代码
- 简单 g++ 内联汇编器中的错误
- 增加精神汇编错误
- STD :: Set在G 汇编后会出现错误
- 汇编错误C 在标题文件中添加函数时
- 为什么我会遇到这些汇编错误
- 代码中的汇编错误
- 汇编:C++堆栈变量地址不同/错误?
- 在新汇编过程中浏览以前的补偿错误