Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查字符串是否重复出现特定字符_C++_String_Search_Boost_Find - Fatal编程技术网

C++ 检查字符串是否重复出现特定字符

C++ 检查字符串是否重复出现特定字符,c++,string,search,boost,find,C++,String,Search,Boost,Find,如何在字符串内部搜索特定字符的多个匹配项(在本例中为句点) 我已经试过修改来自的答案,但我认为我做错了 std::string periodCheck = i.convert_to<std::string>(); char subString = '.'; std::size_t pos = periodCheck.find(subString, 0); int counter; while(pos != std::string::npos){ counter++;

如何在字符串内部搜索特定字符的多个匹配项(在本例中为句点

我已经试过修改来自的答案,但我认为我做错了

std::string periodCheck = i.convert_to<std::string>();
char subString = '.';
std::size_t pos = periodCheck.find(subString, 0);
int counter;
while(pos != std::string::npos){
    counter++;
    if(counter > 1){
        std::cout << "\nError: Multiple periods\n";
        return false;
    }
}
std::string periodCheck=i.convert_to();
字符子字符串='';
std::size\u t pos=periodCheck.find(子字符串,0);
整数计数器;
while(pos!=std::string::npos){
计数器++;
如果(计数器>1){

如果找到句点,那么下一个逻辑步骤也是再次搜索,从下一个字符位置开始

但是,如果您查看代码,您将无法找到它实际再次搜索的任何位置。while循环中没有对
find()
的调用

完全不需要while循环。只需调用
find()
第二次,指定
pos+1
作为第二次搜索的起始位置,然后再次检查结果。如果找到另一个句点,可以将其称为换行符。搜索字符串中的任何剩余句点都不会得到任何结果。您已经找到了答案

std::size_t pos = periodCheck.find(subString, 0);

if (pos != std::string::npos)
{
     pos=periodCheck.find(subString, pos+1);
     if (pos != std::string::npos)
          return false;
}