C++STL中algorithm里find()函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++STL中algorithm里find()函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2400字,纯文字阅读大概需要4分钟。
内容图文
![C++STL中algorithm里find()函数](/upload/InfoBanner/zyjiaocheng/643/43ab4cb5322f4545ad3de4262fd42e87.jpg)
C++STL中algorithm里find()函数
1.string中的运用
1.1函数原型及描述
函数1
size_type find(const string & str, size_type pos = 0) const
形参列表str
:要查找的子字符串string
变量,pos
:要查找的起始位位置
返回为参数size_type
:该子字符串首次出现时其首字符的索引;否则,返回-1
应用
string subString = "Let";
string mainString = "Let life be beautiful like summer flowers,death like autumn leaves";
char ch = subString[0];
int isFindPosition = -1;
isFindPosition = mainString.find(subString, isFindPosition + 1);
cout << "isFindPosition: " << isFindPosition << endl;
输出:isFindPosition:0
函数2
size_type find(const char * s, size_type pos = 0) const
形参列表char *
:要查找的子字符串char *
类型,pos
:要查找的起始位置
返回为参数size_type
:该子字符串首次出现时其首字符的索引;否则,返回-1
函数3
size_type find(const char ch, size_type pos = 0) const
形参列表char
:要查找的子字符char
类型,pos
:要查找的起始位置
返回为参数size_type
:该子字符串首次出现时其首字符的索引;否则,返回-1
应用
string subString = "Let";
string mainString = "Let life be beautiful like summer flowers,death like autumn leaves";
char ch = subString[0];
int isFindPosition = -1;
isFindPosition = mainString.find(ch, isFindPosition + 1);
cout << "isFindPosition: " << isFindPosition << endl;
输出:isFindPosition:0
函数4
size_type find(const char * s, size_type pos = 0, size_type n) const
形参列表char *
:要查找的子字符char*
类型,pos
:要查找的起始位置,n
:的前n个字符组成的子字符串
返回为参数size_type
:该子字符串首次出现时其首字符的索引;否则,返回-1
应用
string mainString = "Let life be beautiful like summer flowers,death like autumn leaves";
char * subString = "beautiful";
char ch = subString[0];
int isFindPosition = -1;
isFindPosition = mainString.find(subString, isFindPosition + 1, 3);
cout << "isFindPosition: " << isFindPosition << endl;
输出:12
2.vector中的运用
2.1函数原型及描述
find(first_position, last_position, find_object)
,返回参数为查找到的位置,可定义变量vector<int>::iterator res;
储存返回的位置。(当然返回类型可以使用auto类型)
当在[first, last]查找到第一个与被查找相等的元素时,即返回查找到的元素位置,返回类型为iterator
,表示元素所在的位置;若未查找到,则返回last的位置。
1.3代码示例
vector<int> numbers = { 1, 2, 9, 1, 2 };
vector<int>::iterator res;
bool isFind;
res = find(numbers.begin(), numbers.end(), 9);
cout << res - numbers.begin() << endl;
小李,不讲道理
发布了10 篇原创文章 · 获赞 1 · 访问量 211
私信
关注
内容总结
以上是互联网集市为您收集整理的C++STL中algorithm里find()函数全部内容,希望文章能够帮你解决C++STL中algorithm里find()函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。