模板类——string类方法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38285131/article/details/83929190

string类

string类是C++标准库的一个重要的部分,也是字符串的一种数据类型,相对于char*字符串它更方便强大,我们不必担心内存是否足够、字符串长度等等,而且作为一个泛型类出现,他集成的操作函数足以完成我们大多数情况下的需要。我们可以用
= 进行赋值操作,== 进行比较,+ 做串联等

基本赋值方法

有以下赋值方式:

    string name("Adam");
	string a, b, c, d,e;
	a = name;
	b = "Road changan";
	c = 'X';
	d = name + b;
	e = { 'h','e','l','l','o' };

字符串简单数据方法

begin() 指向字符串第一个字符的迭代器
end() 超尾值的迭代器,实际指向最后一个元素之后的位置
rbegin() 超尾值的反转迭代器
rend() 指向第一个字符的翻转迭代器
size() 字符串中的元素数
length() 和size作用相同
capacity 给字符串分配的元素数 ,,capacity-size即为字符串还可以附加多少个字符就需分配更多内存
max_size() 字符串类型的最大长度
data() 返回字符串的值
c_str() 返回字符串的值
	string str1 = "hello world";
   cout <<"begin:  "<< *str1.begin() << endl;
	cout <<"end:  "<< *(str1.end()-1) << endl;
	cout <<"rbegin:  " << *str1.rbegin() << endl;
	cout << "rend:  " << *(str1.rend() - 1) << endl;
	cout << "size:  " << str1.size() << endl;
	cout << "length:  " << str1.length() << endl;
	cout << "capacity:  " << str1.capacity() << endl;
	cout << "max_size:  " << str1.max_size() << endl;
	cout << "data:  " << str1.data() << endl;
	cout << "c_str:  " << str1.c_str() << endl;

结果:

begin: h
end: d
rbegin: d
rend: h
size: 11
length: 11
capacity: 15
max_size: 2147483647
data: hello world
c_str: hello world

字符串内存方法

resize(n,c) 如果n>npos,则异常out of range,n>size,则用c来补充字符串,n<size,则阶段字符串
reserve(n) 将字符串设置为n大小,以前的指针,引用,迭代器无效
shrink_to_fit() 将capacity的值与size相同,回收内存
clear() 清空字符串
empty() 判断字符串是否为空
    string str1 = "hello world";
	str1.resize(15, 'b');
	cout << "str1:  " << str1 << endl;
	str1.reserve(30);
	cout << "str1 capacity:" << str1.capacity() << endl;
	str1.shrink_to_fit();
	cout << "str1 size: " << str1.size() << endl;
	cout << "shrink_to_fit" << str1.capacity()<< endl;
	str1.clear();
	cout << "empty:  " << str1.empty() << endl;

结果:

str1: hello worldbbbb
str1 capacity:31
str1 size: 15
shrink_to_fit15
empty: 1

字符串存取

  • at方法
  • []方法
  • front方法
  • back方法
	string str1 = "hello world";
	str1[0] = 'r';
	str1.at(1) = 'a';
	cout << "str1:" << str1 << endl;
	cout << "str1[10]:" << str1[10] << endl;
	cout << "str1[10]:" << str1.at(10) << endl;
	cout << "front: " << str1.front() <<endl;
	cout << "back: " << str1.back() << endl;

str1:rallo world
str1[10]:d
str1[10]:d
front: r
back: d

字符串搜索

  • find() 返回字符串或者字符首字符的位置
  • rfind() 搜索字符串最后一次出现的位置
  • find_first_of() 返回子字符串中的字符首次出现的位置
  • find_last_of() 返回字符串中的某一个字符最后出现的位置
  • find_first_not_of() 返回第一个不位于子字符串的第一个字符的位置
  • find_last_not_of() 返回第一个不位于子字符串的最后一个字符的位置
	string str1 = "hello world";
	cout << "find:" << str1.find("h",5) << endl;
	cout << "find:" << str1.find("h") << endl;
	cout << "rfind:" << str1.rfind("l") << endl;;
	cout << "find_last_of:" << str1.find_last_of("abcde") << endl;
	cout << "find_first_of:" << str1.find_first_of("abcde") << endl;
	cout << "find_last_not_of:" << str1.find_last_not_of("abcde") << endl;
	cout << "find_first_not_of:" << str1.find_first_not_of("abcde") << endl;

find:4294967295 //从第五个位置开始,未找到h
find:0
rfind:9
find_last_of:10
find_first_of:1
find_last_not_of:9
find_first_not_of:0

其他字符串方法

  • compare() 两个字符串比较,或者字符串的子串比较
  • append() 将一个字符串或单个字符串追加到另一个字符串对象后面
  • assign() 将整个字符串,字符串的一部分或由相同字符串组成的字符序列赋值给字符串序列
  • insert() 将string对象或者字符串数组或者几个字符插入到string对象中
  • erase() 从字符串中删除字符
  • replace() 指定了要替换的字符串部分和用于替换的内容
  • copy() 将string对象或者其中一部分复制到指定的字符串数组中
  • swap() 用一个时间恒定的算法来交换两个string对象的内容

—————————————————————————————
compare方法: 当s1在s2前面时返回-1,相同返回0,在后面返回1

	string s1("bellflower");
	string s2("bell");
	string s3("cat");
	string s4("apple");
	int a1 = s1.compare(s3);
	int a2 = s1.compare(0,4,s2,0,4);
	int a3 = s1.compare(s4);
	cout << "a1: " << a1 << endl;//返回 -1
	cout << "a2: " << a2 << endl;//返回 0
	cout << "a3: " << a3 << endl;//返回 1

append方法: 对字符串进行追加

	string s1("bellflower");
	string s2("big");
	s2.append(s1);
	cout << "s2:" << s2 << endl;
	s2.append("abcde");
	cout << "s2:" << s2 << endl;
	cout<<"s2:"<<s1+s2<<endl;

> 输出:
> s2:bigbellflower
s2:bigbellflowerabcde
s2:bellflowerbigbellflowerabcde

assign方法: 对一个字符串进行赋值

	string str1;
	string str2("helloworld");
	str1.assign(str2,0,5);
	cout << "str1:" << str1 << endl;
	str1.assign(str2);
	cout << "str1:" << str1 << endl;
	str1.assign(8, '&');
	cout << "str1:" << str1 << endl;

> 输出:
> str1:hello
str1:helloworld
str1:&&&&&&&&

insert方法: 在字符串的某一个位置插入字符串

	string str2("helloworld");
	str2.insert(0, "big");
	cout << "str2:" << str2 << endl;
	str2.insert(str2.size()," girl like it");

> 输出:
> str2:bighelloworld
str2:bighelloworld girl like it

erase方法: 删除位置n到字符串尾的字符
pop_back() 删除字符串的最后一个字符

	string str2("helloworld");
	str2.erase(6);
	cout << "str2:" << str2 << endl;

> 输出:
> hellow

replace方法 制定要替换的字符串部分和用于替换的内容
开始替换的位置和长度,和要替换的字符串

	string str2("helloworld");
	str2.replace(5,5, "china");
	cout << "str2:" << str2 << endl;
	

> 输出:
> str2:hellochina

swap()方法: 交换两个字符串的内容

	string str2("helloworld");
	string str1("china");
	swap(str1,str2);
	cout << "str2:" << str2 << endl;

> str2:china

猜你喜欢

转载自blog.csdn.net/weixin_38285131/article/details/83929190