二进制表达式的操作数无效?

Invalid operands to binary expressions?

本文关键字:无效 操作数 表达式 二进制      更新时间:2023-10-16

我正在尝试使用下面提供的代码比较字母两个字符串,但不断收到此错误。

此代码是我尝试的代码

#include <vector>
#include <string>
std::vector<int> solve(std::vector<std::string> arr){  
std::string underCase = "abcdefghijklmnopqrstuvwxyz";
std::string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int counter = 0;
for(int i=0; i < arr.size(); i++){
(underCase[i] == arr[i]) ? counter++ : 0;
(upperCase[i] == arr[i]) ? counter++ : 0;
}
return counter;//your code herw
};

这是我得到的错误输出,但我似乎不明白。

main.cpp:13:19: error: invalid operands to binary expression ('int' and 'value_type' (aka 'std::__1::basic_string<char>'))
(underCase[i] == arr[i]) ? counter++ : 0;
~~~~~~~~~~~~ ^  ~~~~~~
main.cpp:14:19: error: invalid operands to binary expression ('int' and 'value_type' (aka 'std::__1::basic_string<char>'))
(upperCase[i] == arr[i]) ? counter++ : 0;
~~~~~~~~~~~~ ^  ~~~~~~
main.cpp:16:10: error: no viable conversion from returned value of type 'int' to function return type 'std::vector<int>'
return counter;//your code herw
^~~~~~~

在这里:

underCase[i] == arr[i]

您正在尝试将char(std::string的元素(与整个std::string(std::vector<std::string>的元素(进行比较。

似乎您可能想要:

underCase[i] == upperCase[i]

或类似的东西:

underCase[?] == arr[i][?]

此外,最好使用正确的ifs 而不是条件运算符:

if (upperCase[i] == upperCase[i])
counter++;
  1. 返回类型与函数的返回类型不匹配。 整数和矢量
  2. 您没有在形式参数中模板化向量的类型。

修复这两件事可以让它在我的机器上运行。无论目的是什么。