我试图迭代一个字符串向量,并在字符串的每个字符上:
但我得到一个错误:C++禁止指针和整数之间的比较.
In member function ‘int LetterStrings::sum(std::vector<std::basic_string<char> >)’:
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
#include<iostream>
#include<vector>
#include<typeinfo>
#include<string>
using namespace std;
class LetterStrings {
public:
int sum(vector <string> s) {
int i, j = 0;
int count = 0;
for(i=0;i<s.size();i++) {
for(j=0;j<s[i].length();j++) {
if(s[i][j] != "-") {
count ++;
}
}
}
return count;
}
};
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我,我的代码有什么问题.
**我对C++很陌生.
你的问题在这里:
if(s[i][j] != "-")
Run Code Online (Sandbox Code Playgroud)
它应该是:
if(s[i][j] != '-') // note the single quotes - double quotes denote a character string
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
404 次 |
| 最近记录: |