首页 > 编程语言 > 详细

C++ string查找示例

时间:2014-02-25 17:21:11      阅读:305      评论:0      收藏:0      [点我收藏+]

查找两个字符串中的公共字符;

返回公共字符的索引;

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s = "**Gteate Wall**!";
	string t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	cout<<"s: "<<s<<endl;
	cout<<"t: "<<t<<endl;

	int first=s.find_first_of(t);

	if(first == string::npos){
		cout<<"s中所有字符均不在t中"<<endl;
	}else {
		cout<<"s中出现在t中的字符的第一个字符:"<<s[first]<<endl;
	}

	int last = s.find_last_of(t);
	if(last == string::npos){
		cout<<"s中所有字符均不在t中"<<endl;
		return 1;
	}else {
		cout<<"s中出现在t的字符的最后一个字符:"<<s[last]<<endl;
		return 1;
	}

}


C++ string查找示例

原文:http://blog.csdn.net/slience_perseverance/article/details/19842027

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!