C++string类常用的操作函数

#include <iostream>
#include <string>//必须包含该C++头文件

using namespace std;

int main()
{
	//string对象的初始化,也就是构造函数与析构函数方面的知识
	/*
	string s;  //生成一个空字符串s
	string s(str); //拷贝构造函数 生成str的复制品
	string s(str, stridx); //将字符串str内“始于位置stridx”的部分当作字符串的初值
	string s(str, stridx, strlen); //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
	string s(cstr); //将C字符串作为s的初值
	string s(chars, chars_len); //将C字符串前chars_len个字符作为字符串s的初值。
	string s(num, c); //生成一个字符串,包含num个c字符
	string s(beg, end); //以区间beg;end(不包含end)内的字符作为字符串s的初值
	s.~string(); //销毁所有字符,释放内存
	*/

	//string类的begin,end,rbegin,rend的用法,返回的是迭代器,不是简单的数组下标,迭代器,反向迭代器
	string str;
	str = "abcdefg higklmn";
	string::iterator beginIter = str.begin();
	string::iterator endIter = str.end();
	string::reverse_iterator rbeginIter = str.rbegin();
	string::reverse_iterator rendIter = str.rend();

	cout << *beginIter << endl;
	//cout << *endIter << endl;//错误,end()函数指向字符串末尾的\0的后面一个位置,无法输出
	cout << *(endIter - 1) << endl;//这就对了

	cout << *rbeginIter << endl;//同上,只不过是反过来的而已
	cout << *(rendIter - 1) << endl;

	for (string::iterator iter = beginIter; iter != endIter; ++iter)//用迭代器与反向迭代器遍历
		cout << *iter;
	cout << endl;
	for (string::reverse_iterator rev_iter = rbeginIter; rev_iter != rendIter; ++rev_iter)
		cout << *rev_iter;
	cout << endl;

	//size()与length()函数,都是返回字符串的长度(不包括尾部的‘\0’)
	int size = str.size();
	int length = str.length();
	cout << size << " " << length << endl;
	//字符串可能的最大大小max_size()
	int maxSize1 = str.max_size();
	cout << maxSize1 << endl;
	//string s1;
	//int maxSize2 = s1.max_size();
	//cout << maxSize2 << endl;

	//字符串的容量capacity()
	cout << str.capacity() << endl;
	string s2;
	cout << s2.capacity() << endl;

	//判空empty()
	bool isEmpty = str.empty();
	if (isEmpty) cout << "为空!" << endl;
	else cout << "不为空!" << endl;
	string s3;
	isEmpty = s3.empty();
	if (isEmpty) cout << "为空!" << endl;
	else cout << "不为空!" << endl;

	//operator[]	取第几个元素,相当于数组
	cout << str[0] << endl;
	for (int i = 0; i < str.size(); ++i)
		cout << str[i];
	cout << endl;

	//标准库的string类提供了3个成员函数来从一个string得到c类型的字符数组:c_str()、data()、copy(p, n)。
	const char* c;
	string s4 = "1234";
	c = s4.c_str();
	cout << c << endl; //输出:1234  
	s4 = "abcd";
	cout << c << endl; //输出:abcd
	const char* c1;
	string s5 = "1234";
	c1 = s5.data();
	cout << c1 << endl; //输出:1234  
	s5 = "abcd";
	cout << c1 << endl; //输出:abcd

	//operator=	赋值操作符,字符串可以直接赋值
	string s6 = "hahahaha";
	string s7;
	s7 = s6;
	cout << s7 << endl;

	//reserve函数	函数reserve()将字符串的容量设置为至少size. 
	//如果size指定的数值要小于当前字符串中的字符数(亦即size < this→size()), 容量将被设置为可以恰好容纳字符的数值
	string s8 = "1234";
	cout << s8.capacity() << endl;
	s8.reserve(32);//10,20,30,40,50等等
	cout << s8.capacity() << endl;

	//swap	交换函数,交换两个字符串
	string s9 = "aaa";
	string s10 = "bbb";
	swap(s9, s10);
	cout << s9 << " " << s10 << endl;

	//reverse函数,字符串翻转
	string s11 = "123";
	reverse(s11.begin(), s11.end());
	cout << s11 << endl;
	reverse(s11.begin(), s11.begin() + 3);
	cout << s11 << endl;

	//insert函数
	//string的成员函数insert有以下多种重载:
	//string &insert(int p0, const char *s); ——在p0位置插入字符串s
	//string &insert(int p0, const char *s, int n); ——在p0位置插入字符串s的前n个字符
	//string &insert(int p0, const string &s); ——在p0位置插入字符串s
	//string &insert(int p0, const string &s, int pos, int n); ——在p0位置插入字符串s从pos开始的连续n个字符
	//string &insert(int p0, int n, char c);//在p0处插入n个字符c
	//iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
	//void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符
	//void insert(iterator it, int n, char c);//在it处插入n个字符c

	string s12 = "aaaaaaaaa";
	char* s13 = "bbb";
	s12 = s12.insert(0, s13);
	s12 = s12.insert(1, s13, 2);
	string s14 = "ccc";
	s12 = s12.insert(3, s14);
	string s15 = "dddde";
	s12 = s12.insert(5, s15, 2, 3);
	s12 = s12.insert(6, 3, 'f');
	string::iterator iter = s12.begin();
	iter++;
	string::iterator iter2 = s12.insert(iter, 'g');
	cout << *iter2 << endl;
	cout << s12 << endl;

	//append函数是向string 的后面追加字符或字符串
	//(1)向string 的后面加C - string
	string s16("Hello "); // s=”Hello ”
	const char* c3 = "Out There ";
	s16.append(c3); // s=”Hello Out There”
	cout << s16 << endl;
	//(2)向string 的后面加C-string 的一部分
	string s17("Hello "); // s=”Hello ”
	const char *c4 = "Out There ";
	s17.append(c4, 3); // s=”Hello Out”
	cout << s17 << endl;
	//(3)向string 的后面加string(有两种方法)
	string s18("Hello "), s19("Wide "), s20("World ");
	s18.append(s19); // s1=”Hello Wide”
	s18 += s20; // s1=”Hello Wide World”
	cout << s18 << endl;
	//(4)向string 的后面加string 的一部分 ---A
	string s21("Hello "), s22("Wide World ");
	s21.append(s22, 5, 5); // s1=”Hello World”
	cout << s21 << endl;
	//(5)向string 的后面加string 的一部分 ---B
	string str1f("Hello "), str2f("Wide World");
	str1f.append(str2f.begin() + 5, str2f.end());
	cout << str1f << endl;
	//(6)向string 的后面加多个字符
	string str1e("Hello ");
	str1e.append(4, '!'); // s1=”Hello !!!!”
	cout << str1e << endl;

	//push_back,类似于vector中的在尾部添加元素
	str1e.push_back(')');
	cout << str1e << endl;

	//赋值运算符=,等于,不等于大于小于,+=,<<等都能直接使用

	//erase函数,删除字符串中字符的函数
	/*erase函数的原型如下:
		(1)string& erase(size_t pos = 0, size_t n = npos);
		(2)iterator erase(iterator position);
		(3)iterator erase(iterator first, iterator last);
	也就是说有三种用法:
		(1)erase(pos, n); 删除从pos开始的n个字符,比如erase(0, 1)就是删除第一个字符
		(2)erase(position); 删除position处的一个字符(position是个string类型的迭代器)
		(3)erase(first, last); 删除从first到last之间的字符(first和last都是迭代器)
	*/
	str1e.erase(0, 2);
	cout << str1e << endl;
	str1e.erase(str1e.begin());
	cout << str1e << endl;
	str1e.erase(str1e.begin() + 3, str1e.end() - 2);//不包括最后一个迭代器
	cout << str1e << endl;

	//clear()函数,清空字符串
	str1e.clear();
	cout << str1e.size() << endl;//为0

	//resize()函数,分配空间大小
	str1e.resize(10);
	cout << str1e.length() << endl;//空间大小为10

	//assign()函数,C++ string 类的成员函数,用于拷贝、赋值操作,
	//它们允许我们顺次地把一个 string 对象的部分内容拷贝到另一个 string 对象上
	/*
	string &operator=(const string &s);               把字符串s赋给当前字符串
	string &assign(const char *s);                 用c类型字符串s赋值
	string &assign(const char *s, int n);              用c字符串s开始的n个字符赋值
	string &assign(const string &s);                 把字符串s赋给当前字符串
	string &assign(int n, char c);                  用n个字符c赋值给当前字符串
	string &assign(const string &s, int start, int n);       把字符串s中从start开始的n个字符赋给当前字符串
	string &assign(const_iterator first, const_itertor last);  把first和last迭代器之间的部分赋给字符串
	*/
	char* c5 = "aaa";
	string s23;
	s23.assign(c5);
	cout << s23 << endl;
	char* c6 = "bbbbbb";
	s23 = s23.assign(c6, 3);
	cout << s23 << endl;
	string s24 = "ccc";
	s23 = s23.assign(s24);
	cout << s23 << endl;
	s23.assign(3, 'd');
	cout << s23 << endl;
	s23 = s23.assign("eeee", 0, 3);
	cout << s23 << endl;
	string s25 = "fff";
	s23 = s23.assign(s25.begin(), s25.end());
	cout << s23 << endl;

	//repalce函数,用于替换
	/*用法一:
	*用str替换指定字符串从起始位置pos开始长度为len的字符
	*string& replace (size_t pos, size_t len, const string& str);
	*/
	string line1 = "this@ is@ a test string!";
	line1 = line1.replace(line1.find("@"), 1, ""); //从第一个@位置替换第一个@为空  
	cout << line1 << endl;
	/*用法二:
	*用str替换 迭代器起始位置 和 结束位置 的字符
	*string& replace (const_iterator i1, const_iterator i2, const string& str);
	*/
	string line2 = "this@ is@ a test string!";
	line2 = line2.replace(line2.begin(), line2.begin() + 6, "heihei ");  //用str替换从begin位置开始的6个字符  
	cout << line2 << endl;
	/*用法三:
	*用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
	*string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
	*/
	string line3 = "this@ is@ a test string!";
	string substr = "12345";
	line3 = line3.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line  
	cout << line3 << endl;
	/*用法四:string转char*时编译器可能会报出警告,不建议这样做
	*用str替换从指定位置0开始长度为5的字符串
	*string& replace(size_t pos, size_t len, const char* s);
	*/
	string line4 = "this@ is@ a test string!";
	char* str111 = "12345";
	line4 = line4.replace(0, 5, str111); //用str替换从指定位置0开始长度为5的字符串  
	cout << line4 << endl;
	/*用法五:string转char*时编译器可能会报出警告,不建议这样做
	*用str替换从指定迭代器位置的字符串
	*string& replace (const_iterator i1, const_iterator i2, const char* s);
	*/
	string line5 = "this@ is@ a test string!";
	char* str222 = "12345";
	line5 = line5.replace(line5.begin(), line5.begin() + 9, str222); //用str替换从指定迭代器位置的字符串  
	cout << line5 << endl;
	/*用法六:string转char*时编译器可能会报出警告,不建议这样做
	*用s的前n个字符替换从开始位置pos长度为len的字符串
	*string& replace(size_t pos, size_t len, const char* s, size_t n);
	*/
	string line6 = "this@ is@ a test string!";
	char* str333 = "12345";
	line6 = line6.replace(0, 9, str333, 4);  //用str的前4个字符替换从0位置开始长度为9的字符串  
	cout << line6 << endl;
	/*用法七:string转char*时编译器可能会报出警告,不建议这样做
	*用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串
	*string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
	*/
	string line7 = "this@ is@ a test string!";
	char* str444 = "12345";
	line7 = line7.replace(line7.begin(), line7.begin() + 9, str444, 4);  //用str的前4个字符替换指定迭代器位置的字符串  
	cout << line7 << endl;
	/*用法八:
	*用重复n次的c字符替换从指定位置pos长度为len的内容
	*string& replace (size_t pos, size_t len, size_t n, char c);
	*/
	string line8 = "this@ is@ a test string!";
	char c111 = '1';
	line8 = line8.replace(0, 9, 3, c111);    //用重复3次的c字符替换从指定位置0长度为9的内容  
	cout << line8 << endl;
	/*用法九:
	*用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容
	*string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
	*/
	string line9 = "this@ is@ a test string!";
	char c222 = '1';
	line9 = line9.replace(line9.begin(), line9.begin() + 9, 3, c222);    //用重复3次的c字符替换从指定迭代器位置的内容  
	cout << line9 << endl;

	//find函数,查找,返回目标的头位置的索引,否则返回string::npos
	//size_type find(const string & str, size_type pos = 0) const;
	//size_type find(const char * s, size_type pos = 0) const;
	//size_type find(const char * s, size_type pos = 0, size_type n) const;
	//size_type find(const char ch, size_type pos = 0) const;
	//rfind()查找子字符串或字符最后一次出现的位置。
	//find_first_of()方法在字符串中查找参数中任何一个字符或字符串首次出现的位置
	//find_last_of()方法在字符串中查找参数中任何一个字符或字符串最后一次出现的位置
	string s111("1a2b3c4d5e6f7jkg8h9ija2b3c4d5e6f7g8ha9i");
	string::size_type position;
	position = s111.find("jk", 0);//返回第一个出现的位置,从0开始
	if (position != string::npos) cout << position << endl;
	else cout << "Can't find!" << endl;

	string flag = "c";
	position = s111.find_first_of(flag);
	printf("s.find_first_of(flag) is :%d\n", position);
	position = s111.find_last_of(flag);
	printf("s.find_last_of(flag) is :%d\n", position);
	//从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
	position = s111.find("b", 5);
	cout << "s111.find(b,5) is : " << position << endl;
	position = s111.rfind("c");//查找最后一个出现的位置,也就是反向查找第一次出现的位置,
	                         //通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一。
	if (position != string::npos) cout << position << endl;

	flag = "a";//查找所有的位置
	position = 0;
	int i = 1;
	while ((position = s111.find(flag, position)) != string::npos)
	{
		cout << "position  " << i << " : " << position << endl;
		position++;
		i++;
	}

	//substr函数,得到子串,三种用法
	string x = "Hello_World";
	cout << x.substr() << endl;//截取整个串
	cout << x.substr(5) << endl;//从位置5开始截取子串
	cout << x.substr(0, 5) << endl;//从位置0开始,截取长度为5的子串

	//compare()函数,前面减去后面的ASCII码,>0返回1,<0返回-1,相同返回0
	string str1("green apple");
	string str2("red apple");
	string str3("apple");
	if (str3.compare("apple") == 0)//直接整个比较
		cout << str3 << " is an apple!" << endl;
	if (str1.compare(str2) != 0)
		cout << str1 << " is not " << str2 << endl;
	if (str1.compare(6, 5, "apple") == 0)//从位置6开始比较,比较5个字符
		cout << "still, " << str1 << " is an apple!" << endl;
	if (str2.compare(str2.size() - 5, 5, "apple") == 0)
		cout << "and " << str2 << " is also an apple!" << endl;
	if (str1.compare(6, 5, str2, 4, 5) == 0)//从str1的位置6,str2的位置4开始比较
		cout << "therefore, both are apples!" << endl;
	string a("aBcdef");
	string b("AbcdEf");
	string c123("123456");
	string d("123dfg");
	//下面是各种比较方法
	//前面减去后面的ASCII码,>0返回1,<0返回-1,相同返回0
	//完整比较a和b
	int m = a.compare(b);
	//“Bcdef”和“AbcdEf”的比较,比较a和b的从1到5位,b剩下的不要比较
	int n = a.compare(1, 5, b);
	//“Bcdef”和“Ef”的比较
	int p = a.compare(1, 5, b, 4, 2);
	//"123"和“123”的比较
	int q = c123.compare(0, 3, d, 0, 3);
	cout << "m=" << m << ",n=" << n << ",p=" << p << ",q=" << q << endl;

	//getline函数,从输入流中读入一行,以换行符\n结束


	system("pause");
	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值