C++中string使用

所有解释已经在程序中标明,不再单独解释

代码:

//如何使用C++标准库类型中的string
#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int main()
{
	//1、构造函数
	cout << "***********************string类的构造函数***********************" << endl;
	string nullString;  //空字符串
	string str1("Hello, string"); //给定常量字符串的一个拷贝
	string str2(str1); //给定字符串的拷贝
	string str3(10, 'c'); //连续十个字符'c'
	string str4("Hello, string", 5); //取前五个字符
	string str5(str1, 7, 6);//从下标(下标从0开始)为7的字符起,取6个字符
 
	vector<char> vec;
	vec.push_back('a');
	vec.push_back('b');
	vec.push_back('c');
 
	string str6(vec.begin(), vec.end()); //把input_iterator迭代器[start,end)间的字符拷贝给str6
 
	cout << nullString << endl;
	cout << str1 << endl;
	cout << str2 << endl;
	cout << str3 << endl;
	cout << str4 << endl;
	cout << str5 << endl;
	cout << str6 << endl;
 
	//2、string类的字符操作
	cout << "***********************string类的字符操作***********************" << endl;
	string str("0123456789");
	str[0] = 'a';   //[]不提供越界检查  对于特殊的str[11]输出‘ ’ (在VS2012中[]也提供了越界检查)
	str.at(1) = 'b'; //at()提供越界检查 对于特殊的str.at(11)提示越界
	cout << str << endl;  
 
	cout << str.data() << endl;  //data()返回的是一个非null终止的C字符数组,返回值类型为const char*
	cout << str.c_str() << endl;  //c_str返回一个以null终止的C字符串,返回类型为const char*
 
	char s[100];
	cout << str.copy(s, 20, 0) << endl;  //把str中从0开始的20个字符拷贝到字符数组s中,返回值为实际拷贝的字符数目
	cout << s << endl;  //没有'\0'结尾哦
 
	//3、string类的特性
	cout << "***********************string类的特性***********************" << endl;
	cout << str.capacity() << endl;
	cout << str.max_size() << endl; //string对象可以存放的最大字符串长度
	cout << str.size() << endl;//返回字符串尺寸
	cout << str.length() << endl;//返回字符串长度
	cout << str.empty() << endl; //判断str是否为空字符串,返回值为bool型
 
	str.resize(20, 'a'); //重设字符串str的尺寸为20,不足部分用'a’填充
	cout << str.size() << endl;
	cout << str.length() << endl;
	cout << str << endl;
 
	//4、string类输入
	cout << "***********************string类的输入***********************" << endl;
	cout << "string类重载operator>>用于输入,重载operator<<用于输出" << endl;
	string tmp;
	while (getline(cin, tmp))  //以Ctrl+Z结束输入,这个是从标准输入流cin中输入字符的,以换行符'\n'分开
	{
		cout << tmp << endl;
	}
 
	//5、string类赋值
	cout << "***********************string类的赋值***********************" << endl;
	string forAssign("forAssign");
	str = forAssign;  //赋值操作符
	const char* c_string = "c_string for c++ string";
	str.assign(c_string);  //用c类型字符串给str赋值
	str.assign(c_string, 8); //把c_string开头8个字符赋值给str
	cout << str << endl;
 
	str.assign(forAssign); //把字符串forAssign赋值给str
	str.assign(10, 'a'); //把十个字符'a'赋值给str
	cout << str << endl;
	str.assign(forAssign, 0, 4);//把forAssign从下标0开始的4个字符赋值给str
	cout << str << endl;
	str.assign(vec.begin(), vec.end()); //把迭代器[begin, end)中的部分赋值为str
	cout << str << endl;
 
	//6、string类连接
	cout << "***********************string类连接***********************" << endl;
	string tmpAppend("append");
	str += tmpAppend;
	str.append("nihao");
	char cAppend[] = "c string for append";
	str.append(cAppend);
	str.append(cAppend, 5);  //将cAppend前5个字符加到str后面
	str.append(tmpAppend);
	str.append(tmpAppend, 0, 5); //将tmpAppend从下标0开始的5个字符加到str后
	str.append(10, 'd');  //在str后加上10个字符'd'
	str.append(vec.begin(), vec.end());
 
	cout << str << endl;
 
	//7、string的比较
	cout << "***********************string的比较***********************" << endl;
	str1 = "for compare";
	str2 = "for compare";
	if(str1==str2) //运算符==,>,<,>=,<=,!=均被重载
	{
		cout << "str1==str2" << endl;
	}
	else
	{
		cout << "str!=str2" << endl;
	}
 
	//compare的返回值为1,0,-1
	cout << str1.compare(str2) << endl;
	cout << str1.compare(0, 3, str2) << endl; //str1从下标0开始的3个字符组成的字符串与str2比较
	cout << str1.compare(0, 3, str2, 0, 3) << endl; //str从下标0开始的3个字符组成的字符串与str2从下标0开始的3个字符组成的串比较
 
	char cCompare[] = "for compare";
	cout << str1.compare(cCompare) << endl;
	cout << str1.compare(0, 3, cCompare) << endl;
	cout << str1.compare(0, 3, cCompare, 0, 3) << endl;
 
	//7、string的子串
	cout << "***********************string的子串***********************" << endl;
	str = "I am sub string in main string";
	string subStr = str.substr(0, 15);
	cout << subStr << endl;
 
	//8、string的交换
	cout << "***********************string的交换***********************" << endl;
	str1 = "I am str1";
	str2 = "I am str2";
	str1.swap(str2);
	cout << str1 << endl;
	cout << str2 << endl;
 
	//9、string的查找
	cout << "***********************string的查找***********************" << endl;
	cout << "查找成功返回所在位置,查找失败返回string::npos = " << (int)string::npos << endl;
	str = "0123456789";
	cout << "从left侧查找:" << endl;
	cout << str.find('3', 0) << endl;
	cout << str.find("567", 0) << endl;
	cout << str.find('a', 0) << endl;
	cout << str.find("456a", 2, 3) << endl; //从str的下标2开始查找字符串"456a"前3个字符在str中的位置
 
	string tmpFind("789");
	cout << str.find(tmpFind, 0) << endl;
 
	cout << "从right侧查找:" << endl;
	cout << str.rfind('4', 6) << endl;//从下标6位置从后向前查找字符'4'
	cout << str.rfind("78", 8) << endl;//从下标8位置从后向前查找"7,8"
	cout << str.rfind('9', -1) << endl;//这个也验证了string::npos == -1
 
	cout << str.rfind("56789", 8, 2) << endl;//从str的下标8的位置从后向前查找"56789"前两个字符组成的字符串的位置
	cout << str.rfind(tmpFind, 7) << endl; //只要tmpFind的第一个字符出现在str字符串0-7范围内就可以找到
 
	cout << "find_first_of : 查找当前串中那个字符最先在后面给定的字符串中出现,返回第一个出现在当前串中的位置" << endl;
	str = "abcabc";
	cout << str.find_first_of('a', 0) << endl;//从str下标0开始查找字符'a'第一次出现的位置
	cout << str.find_first_of("tybh", 0) << endl; //查找str中第一个在"tybh"中出现的字符位置
	cout << str.find_first_of("bca", 0, 2) << endl;//从str下标0开始查找第一个出现在字符串"bca"前2个字符组成的字符串"bc"的位置
	cout << str.find_first_of(tmpFind, 0) << endl;
 
	cout << "find_first_not_of : 查找当前串中那个字符最先不出现在后面给定的字符串中出现,返回第一个不出现在当前串中的字符的位置" << endl;
	str = "abcabcdf";
	cout << str.find_first_not_of('a', 0) << endl;//从str下标0开始查找字符'a'第一次不出现的位置
	cout << str.find_first_not_of("atybh", 0) << endl; //查找str中第一个不在"tybh"中出现的字符位置
	cout << str.find_first_not_of("bca", 0, 3) << endl;//从str下标0开始查找第一个不出现在字符串"bca"前2个字符组成的字符串"bc"的位置
	cout << str.find_first_not_of(tmpFind, 0) << endl;
 
	cout << "find_last_of : 从后向前查找当前串中那个字符最先在后面给定的字符串中出现,返回第一个出现在当前串中的位置" << endl;
	str = "abcdeabc";
	cout << str.find_last_of('a', string::npos) << endl; //从后向前查找第一个出现字符‘a'的位置,也即最后一个'a'的位置
	cout << str.find_last_of("ab", string::npos) << endl; //从后向前查找第一出现在"ab"中的字符位置,也即最后一个出现在"ab"中的字符位置
	cout << str.find_last_of("abc", 7, 2) << endl; //从下标为7的字符向前查找第一个不在"abc"前两个字符组成的字符串中的字符位置
	cout << str.find_last_of(tmpFind, string::npos) << endl;
 
	cout << "find_last_not_of : 从后向前查找当前串中那个字符最先不在后面给定的字符串中出现,返回第一个不出现在当前串中的位置" << endl;
	str = "abcdfabc";
	cout << str.find_last_not_of('c', string::npos) << endl;
	cout << str.find_last_not_of("abc", string::npos) << endl;
	cout << str.find_last_not_of("abc", string::npos, 2) << endl;
	cout << str.find_last_not_of(tmpFind, string::npos) << endl;
 
	//10、string的替换
	cout << "***********************string的替换***********************" << endl;
	str = "0123456789";
	char* c_replace = "C_Replace";
	string cp_replace("CP_Replace");
	cout << str.replace(0, 4, c_replace) << endl;//从下标0开始删除4个字符,然后在下标0处 插入字符串c_replace
	str = "0123456789";
	cout << str.replace(0, 4, c_replace, 5) << endl;//从下标0开始删除4个字符,然后在下标0处 插入字符串c_replace的前5个字符
	str = "0123456789";
	cout << str.replace(0, 4, cp_replace) << endl;
	str = "0123456789";
	cout << str.replace(0, 4, cp_replace, 3, 4) << endl;//从下标0开始删除4个字符,然后在下标0处,插入cp_replace从下标3开始的四个字符
	str = "0123456789";
	cout << str.replace(0, 4, 10, 'a') << endl;//从下标0开始删除4个字符,然后在下标0处 插入10个字符'a'
 
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, "123") << endl;
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, "1234", 2) << endl; //把str中[str.begin(), str.begin()+3)的字符替换为“1234”的前2个字符
	subStr = "123";
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, subStr) << endl;
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, 10, 'f') << endl;//把str中[str.begin(), str.begin()+3)的字符替换为10个字符'f'
	str = "abc67abc";
	cout << str.replace(str.begin(), str.end(), subStr.begin(), subStr.end()) << endl;
	cout << str.replace(str.begin(), str.end(), str.begin(), str.end()) << endl;
 
	//11、string的插入
	cout << "***********************string的插入***********************" << endl;
	str = "abcdefg";
	subStr = "123";
	cout << str.insert(0, "123") << endl;
	cout << str.insert(0, "123", 2) << endl;//从下标0开始插入字符串"123"的前2个字符
	cout << str.insert(0, subStr) << endl;
	cout << str.insert(0, subStr, 2, 1) << endl;//从下标0开始出入字符串subStr从下标2开始的1个字符
 
	cout << str.insert(0, 10, '0') << endl;
	str = "abcefg";
	str.insert(str.begin(), '1'); //返回的是出入后迭代器的位置
	cout << str << endl;
 
	str = "abcefg";
	str.insert(str.begin(), str.begin()+2, str.begin()+4); //在str.begin()处插入[str.begin()+2, str.begin()+4)的字符
	cout << str << endl;
	str.insert(str.begin(), 10, 'f'); //在str.begin()处插入10个'f'字符
 
	//12、string的删除
	cout << "***********************string的删除***********************" << endl;
	str = "0123456789";
	str.erase(str.begin(), str.begin()+3);
	str.erase(str.begin());
	str = "0123456789";
	cout << str.erase(0, 3) << endl;
 
	string::iterator ite = str.begin();
 
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41848006/article/details/81805590