C++ string.find() 不返回 -1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15740829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:43:55  来源:igfitidea点击:

string.find() doesn't return -1

c++

提问by eoLithic

The code below is simple. As I know, if string::find() didn't find matches it returns -1. But for some reasons the code below doesn't work. Everytime I run this code I get endless loop. Thank you for help!

下面的代码很简单。据我所知,如果 string::find() 未找到匹配项,则返回 -1。但由于某些原因,下面的代码不起作用。每次我运行这段代码时,我都会陷入无限循环。谢谢你的帮助!

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string text;
    text = "asdasd ijk asdasd";
    string toReplace = "ijk";
    cout<<text<<endl;
    int counter = 0;
    while ( text.find(toReplace) != -1)
        counter++;

    cout<<counter<<endl;

    system("pause");
}

回答by Memento Mori

Aside from the other answers which are completely correct, I just wanted to add that your while loop would have produced an endless loop anyway. For example:

除了完全正确的其他答案之外,我只想补充一点,您的 while 循环无论如何都会产生无限循环。例如:

while(text.find(toReplace) != std::string::npos)
    counter++;

will be an endless loop because it will keep trying to find the toReplacestring in textand it will always find it (that's because find starts from the beginning of the string each time). This is probably not what you intended.

将是一个无限循环,因为它会不断尝试在其中查找toReplace字符串text并且总是会找到它(这是因为 find 每次都从字符串的开头开始)。这可能不是你想要的。

回答by Armen Tsirunyan

std::string::findreturns std::string::nposif the searched substring is not found, not -1. The exact value of nposis implementation-defined, so use npos, as in

std::string::findstd::string::npos如果未找到搜索的子字符串,则返回,而不是-1。的确切值npos是实现定义的,因此请使用npos,如

while ( text.find(toReplace) != std::string::npos)

Come to think of it, findcouldn't return -1 even if it wanted to because the return type of find is specified to be std::size_twhich is an unsignedtype.

试想想它,find不能返回-1即使它想,因为发现的返回类型被指定为std::size_t这是一个无符号的类型。

Additionally, find will alwayssearch for the firstoccurrence of the substring, no matter how many times you call it. If you want to iterate through all the occurrences you should use the overload of findwhich takes a second parameter - the position from which to start searching.

此外,无论您调用多少次, find 都将始终搜索第一次出现的子字符串。如果你想遍历所有的出现,你应该使用它的重载,find它带有第二个参数 - 开始搜索的位置。

回答by Kiril Kirov

Whoever told you this or wherever you read it, it lied to you.

不管是谁告诉你的,或者你在哪里读到的,都是骗你的。

If std::string::findfails, it returns std::string::npos, which is not -1.

如果std::string::find失败,则返回std::string::npos,而不是-1

You should check the documentation about such things, when you're not sure.

当您不确定时,您应该查看有关此类内容的文档。

So, your whilewill be something like:

所以,你while会是这样的:

while ( std::string::npos != text.find(toReplace) )


Regarding your comment:

关于你的评论:

UPDATE: I tried to use while ( text.find(toReplace) != string::npos ) but I still get endless loop :( – user2167403 10 secs ago

更新:我尝试使用 while ( text.find(toReplace) != string::npos ) 但我仍然无限循环 :( – user2167403 10 secs ago

You should really learn to read the documentation. Use a variable to store the last result of std::string::find(different from std::string::npos) and use std::string::find's second parameter - pos( by passing value - last_match_position + 1).

你真的应该学会阅读文档。使用变量存储std::string::find(不同于std::string::npos)的最后一个结果并使用std::string::find的第二个参数 - pos(通过传递值 - last_match_position + 1)。

Omitting the second parameter, std::string::findalways starts from the beginning of the string, which causes the endless loop.

省略第二个参数,std::string::find总是从字符串的开头开始,这会导致无限循环。

回答by Pavel Krasnopevtsev

In the code snippet you provided textvariable contains substring "ijk" which is stored in the toReplacevariable. As long as in while cycle neither textor toReplacevariables are changed, find method will always return not a -1 value which is the condition for while cycle to continue.

在您提供的代码片段中,text变量包含存储在变量中的子字符串“ijk” toReplace。只要在while循环既不texttoReplace变量发生变化,找到方法将总是返回不是一个值-1这是while循环继续的条件。

As already metioned in other comments you should check not for -1 but for std::string::npos.

正如在其他评论中已经提到的,您不应该检查 -1 而是检查std::string::npos.

回答by Ed Heal

It does help to read the manual page (string::npos is the answer).

它确实有助于阅读手册页(string::npos 是答案)。

See http://www.cplusplus.com/reference/string/string/find/

http://www.cplusplus.com/reference/string/string/find/