【发布时间】:2017-03-12 17:51:11
【问题描述】:
我已经彻底(我认为)查看了有关此主题的所有类似问题,但找不到适合我的解决方案。
我需要计算字符串向量 bookFile(每个字符串包含多个单词和空格)中特定字母 (ch) 的数量,并测试 ch 出现的次数是否大于 1。
这是我现在拥有的(字符存储在向量中,messageFile):
char ch = messageFile[i];
if(count(bookFile.begin(), bookFile.end(), ch) > 1){
// do something if there are more than 1
}
但是我在编译时遇到了这个错误:
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion) bcencode c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 3310
我还是 c++ 新手,所以我不确定出了什么问题。
更新
我最终用这个来计算向量中的字母数量。谢谢@Kolyan1
int countLetters(vector<string> &b, char ch) {
int counter = 0;
for (auto it = b.begin(); it != b.end(); ++it) {
string temp_string = *it; //not strictly necessary
counter += count(temp_string.begin(), temp_string.end(), ch);
}
return counter;
}
【问题讨论】:
-
我是
string的向量,你正在搜索char,你不应该搜索string吗? -
我需要在字符串中找到一个字母(ch)。
-
字符串?哪一个 ?你有一个完整的
std::vectorstrings -
@P0W vector
他需要双循环一个到迭代器字符串向量,另一个到迭代器字符在字符串中 -
@AnkurJyotiPhukan 是的,我明白了,但我不会盲目地假设,只是后来意识到 OP 意味着完全不同的东西,这种情况经常发生