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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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++;:find(str,ind)返回几个-1_C++_String_Find - Fatal编程技术网

C++ C++;:find(str,ind)返回几个-1

C++ C++;:find(str,ind)返回几个-1,c++,string,find,C++,String,Find,例如,如果我输入“love”作为字符串并搜索字符“o”,我会得到: 找到1,位置:0 找到1,位置:1 找到-1和位置2 角色的最后位置:-1,索引为:3 另一个例子是,如果我输入“我爱你”并搜索字符“v”,我会得到: 找到:4和位置:0 找到:4和位置:1 找到:4和位置:2 发现:4和位置:3 找到:4和位置:4 找到:-1和位置:5 找到:-1和位置:6 找到:-1和位置:7 找到:-1和位置:8 #包括 #包括 使用名称空间std; int main() { 字符串str; c

例如,如果我输入“love”作为字符串并搜索字符“o”,我会得到:

  • 找到1,位置:0
  • 找到1,位置:1
  • 找到-1和位置2
  • 角色的最后位置:-1,索引为:3
另一个例子是,如果我输入“我爱你”并搜索字符“v”,我会得到:

  • 找到:4和位置:0
  • 找到:4和位置:1
  • 找到:4和位置:2
  • 发现:4和位置:3
  • 找到:4和位置:4
  • 找到:-1和位置:5
  • 找到:-1和位置:6
  • 找到:-1和位置:7
  • 找到:-1和位置:8

#包括
#包括
使用名称空间std;
int main()
{
字符串str;
char ch;
int i,发现;
请看一看您使用的变体(4),其内容如下

size_t find (char c, size_t pos = 0) const;
其中参数解释为:

pos
   Position of the first character in the string to be considered in the search.
   If this is greater than the string length, the function never finds matches.
   Note: The first character is denoted by a value of 0 (not 1): A 
   value of 0 means that the entire string is searched.

在返回值下,我们找到

The position of the first character of the first match.
If no matches were found, the function returns string::npos.
让我们看一些例子,让我们以
love
为例,因为它较短,我们看
string(“love”)。查找('o',i)
,其中
i
的范围从
0
2

i=0

i=1

i=2


没有什么错:

返回找到的子字符串的第一个字符的位置,或者如果没有找到这样的子字符串

static const size_type npos = -1;

该函数失败时返回
std::string::npos
(其值-1转换为无符号)。检查该值并退出循环:没有更多匹配项。我应该使用“find(str,ind)”,而不是。因此,当它失败时返回-1。这很奇怪,因为我可以输入:“我爱你”这是因为你循环直到
i
。所以在《我爱你》中,你从第二个“o”开始就不会再重复。事实上,我忘了我把它改为str.length()-1。如果没有str.length()-1(str.length()),我最后会得到-1。谢谢你的帮助。
The position of the first character of the first match.
If no matches were found, the function returns string::npos.
love
^ first considered char
 ^ reported find so return value is 1
love
 ^ first considered char
 ^ reported find so return value is 1
love
  ^first considered char
  -- no match after that position, so return value is string::npos (== -1 most implementations) 
static const size_type npos = -1;