C++——STL——string容器

1 篇文章 0 订阅

string的头文件

#include <string>

string的初始化

1.默认初始化,此时该字符串为空字符串

string s1;

2.s2是s1的副本

string s2(s1)//构造函数

3.等价于s3(s1),则s3是s1的副本

string s3=s1;

4.s4的字面值是"nihao"

string s4("nihao");//构造函数

5.与上行代码是等价的

string s5="nihao"

6.将s6初始化为由连续的n个字符h组成的串

string s6(n,'h');//构造函数

7.调用string的构造函数生成一个临时的string类,再用临时的string类初始化

string s7=string("hello");
string s8(string("hello"));

string的输入

1.cin输入

string s1;
cin>>s1;
cout<<"s1="<<s1<<endl;

缺点:遇到空格便会自动停止,字符串不会记录空格及以后的内容,如下图

 这个时候使用getline就不会出现这样的问题。(注意:使用getline一定要导包)

2.getline输入

string s1;
getline(cin, s1);
cout << "s1=" << s1 << endl;

string容器的大小比较和连接

和char型数组不同,string可以直接使用><==符号来进行比较,同样连接也是

string s1 = "hello";
string s2 = " world";
string s3 = s1 + s2;
cout << s3 << endl;

string中字符的获取

1.传统for循环

string s1="hello"
for (int i = 0; i < s1.size(); i++) {
		cout << s1[i] << "";
	}

2.新特性的for循环

该方法属于c++11的新特性

string s1="hello"
for (auto i:s1) {
		cout <<i<< "";
	}

3.使用迭代器

迭代器类似于指针

string s1 = "hello";
for (auto i = s1.begin(); i != s1.end(); i++) {
	cout << *i << "";
}

string对象的拷贝

1.string s(s1,pos)

string s1="hello world";
string s(s1, 6);
cout << "s=" << s << endl;//world

2.string s(s1,pos,len)

string s1="hello world";
string s(s1, 6, 1);
cout << "s=" << s << endl;//w

3.s.substr ( pos  ,n )

    string s="hello world";
	string s1 = s.substr();
	cout << "s1=" << s1 << endl;
	string s2 = s.substr(0,4);
	cout << "s2=" << s2 << endl;
	string s3 = s.substr(10, 10);
	cout << "s3=" << s3 << endl;
	string s4 = s.substr(11, 5);
	cout << "s4=" << s4 << endl;
	string s5 = s.substr(12, 10);//超出长度,会出现异常,程序无法正常运行
	cout << "s5=" << s5 << endl;

若删除s5后程序运行结果如下:

插入

1.iterator insert ( iterator pos, CharT ch ) 

	string s1="0123456789";
	s1.insert(s1.begin()+5,'a');
	cout << "s1=" << s1 << endl;//01234a56789

因为要插入的内容是char型,所以若是想要插入多个字符,可以利用数组

	string s1="0123456789";
	s1.insert(s1.begin()+5,{'a','b','c'});
	cout << "s1=" << s1 << endl;//s1=01234abc56789

2.void insert ( iterator pos, size_type count, CharT ch)

    string s1="0123456789";
	s1.insert(s1.begin()+5,2,'b');
	cout << "s1=" << s1 << endl;//s1=01234bb56789

3.void insert( iterator pos, InputIt first,InputIt last )

注意:这里的字串区间为【first,last)

    string s1="0123456789";
	s1.insert(s1.begin()+1,s1.begin()+6, s1.begin()+8);
	cout << "s1=" << s1 << endl;//s1=067123456789
	

这里需要注意,输出结果是067123456789

删除

erase

1.erase()

直接将字符串删除为空

2.erase(size_type pos, size_type n)

删除字符串从pos下标开始(包括pos下标)的n个字符

	string s1="0123456789";
	s1.erase(0, 5);
	cout << "s1=" << s1 << endl;//s1=56789
	

3.erase(const_iterator first,const_iterator last)

删除迭代器【first, last)区间的字符

	string s1="0123456789";
	s1.erase(s1.begin()+3, s1.begin() + 5);
	cout << "s1=" << s1 << endl;//s1=01256789

append(末尾)

append只能在string对象的末尾进行插入操作,通过+运算符也可以达到同样效果


	string s1="0123456789";
	s1.append("aaa");
	cout << "s1=" << s1 << endl;//s1=0123456789aaa

替换

1.replace(size_type pos, size_type n, const char* c)

将字符串从下标pos开始删除n个字符,删除后在下标pos处插入c

	const char* ch2 = "abcd";
	string s1="0123456789";
	s1.replace(1, 4, ch2);
	cout << "s1=" << s1 << endl;//s1=0abcd56789

2.replace(size_type pos1, size_type n1, const char* c, size_type n2)

将字符串从下标pos1开始删除n1个字符,删除后在下标pos处插入c的前n2个字符

	const char* ch2 = "abcd";
	string s1="0123456789";
	s1.replace(1, 4, ch2, 1);
	cout << "s1=" << s1 << endl;//s1=0a56789

3.replace(size_type pos, size_type n, string s)

将字符串从下标pos开始删除n个字符,删除后在下标pos处插入s

	string s2 = "abcd";
	string s1="0123456789";
	s1.replace(1, 4,s2);
	cout << "s1=" << s1 << endl;s1=0abcd56789
	

4.replace(size_type pos1, size_type n1, string s, size_type pos2, size_type n2)

将字符串从下标pos1开始删除n1个字符,删除后在下标pos1处插入s从下标pos2开始的n2个字符

	string s2 = "abcd";
	string s1="0123456789";
	s1.replace(1, 4, s2, 2, 2);
	cout << "s1=" << s1 << endl;//s1=0cd56789
	

5.replace(size_type pos1, size_type n1, string s, size_type n2, CharT ch)

将字符串从下标pos1开始删除n1个字符,删除后在下标pos1处插入n2个ch字符

	string s2 = "abcd";
	string s1="0123456789";
	s1.replace(1, 4, 10, '@');
	cout << "s1=" << s1 << endl;//s1=0@@@@@@@@@@56789

判断是否是标点符号

ispunct()

string s1=",01,23,45678,9";
	for (auto i:s1) {
		if (!ispunct(i)) {
			cout << i ;
		}
	}

  • 17
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值