一些字符串函数:append,to_string,stoi

1 append

append是比push_back更方便的在字符串后增加字符串的方式,类似字符串的+。这是string的成员函数。
差异:append只能增加字符串,push_back只能增加字符。

	string s1 = "hello";	//s1 = "hello"
	string s2 = "world";	//s2 = "world"
	string s3 = s1 + s2;	//s3 = "helloworld"

	s1.append(' ');			//Wrong! 不能用append(char)
	s1.append(" ");			//s1 = "hello "
	s1.append(s2);			//s1 = "helloworld"

2 to_string 和stoi

to_string可以将数值转化为字符串,无论是int还是float均可。
stoi可以将字符串转化为数值。
这两个是非标准库的非成员函数,慎用。

	int a = 666;
	string get = to_string(a);	//get = "666"
	int ret = stoi(get);		//ret = 666

猜你喜欢

转载自blog.csdn.net/weixin_42979679/article/details/104387845