STD :: Set在G 汇编后会出现错误

std::set is giving errors after g++ compilation

本文关键字:错误 汇编 Set STD      更新时间:2023-10-16

我正在尝试找到解决方案的解决方案(给定一个数字的数组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是拼写〜的另一种方式,是位的互补操作员。

只需将您的变量重命名为其他东西