STL—string
一、什么是string
我们在C语言中,存储字符数组是用char str[]
来存放字符串,但是用这个存储的时候一般会显得麻烦,还容易产生一些错误,C++在STL中加入了string
类型,使得操作起来更加的方便
要使用string
,需要添加头文件#include <string>
二、string的操作
1.string的定义
定义string
的方式跟基本类型相同:
string str;
- 1
初始化:
string str = "abcd";
- 1
2.string中内容的访问
(1)通过下标访问
可以像字符数组那样去访问string
str.length();
为str的长度
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcd";
for (int i = 0; i< str.length(); i ++ )
cout << str[i];
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出结果为:abcd
这里再说一下输入输出
输入有两种方式,一种是不能读入空格:cin
,另一种是能读入空格:getline(cin, str);
,即如果用cin
读入一个字符串的话,碰到空格就会停止读入,而getline(cin, str);
则是碰到回车(换行)才会停止读入
输出的话可以用cout
来输出,当然也可以通过printf
进行输出,但是过程有点繁琐,这里不介绍
(2)通过迭代器访问
一般通过(1)就可以满足访问的要求,但是string
中的某些函数,比如insert()
,erase()
则要求用迭代器作为参数,所以我们还是要介绍一下迭代器的用法
定义一个迭代器
string::iteratoer it;
- 1
通过上述定义我们就得到了迭代器it
,我们就可以通过*it
去访问string
里的每一位
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcd";
for (string::iterator it = str.begin(); it != str.end(); it ++ )
cout << *it;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
当然我们也可以简写成:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcd";
for (auto it = str.begin(); it != str.end(); it ++ )
cout << *it;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出结果为:abcd
3.string中的函数
(1)operator+=
这是string的加法,可以把两个string直接拼接起来
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcd";
string str2 = "efg";
string str3 = str1 + str2;
cout << str3;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
输出结果为:abcdefg
(2)compare operator
两个string
类型可以直接使用==, !=, <, <=, >, >=
比较大小,比较的规则是字典序
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcd";
string str2 = "efg";
if (str1 > str2) cout << "str1 > str2";
else if (str1 == str2) cout << "str1 == str2";
else cout << "str1 > str2";
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
输出结果为:str1 > str2
(3)length()/size()
length()
返回string
的长度,即存放的字符数,时间复杂度为O(1),size()
与length()
的用法基本相同
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcd";
string str2 = "efg";
cout << str1.size() << ' ' << str2.length();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出结果为:4 3
(4)insert()
这里介绍insert()
的两种写法
insert(pos, string) 在pos
号的位置插入字符串string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcd";
string str2 = "efg";
str1.insert(2, str2);
//在str1[2]处插入str2
cout << str1;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
输出结果为:abefgcd
insert(it, it2, it3) it
为原字符串的欲插入位置,it2
和it3
为待插入字符串的首位迭代器,用来表示[it2, it3)
将被插在it
的位置上
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcd";
string str2 = "efg";
str1.insert(str1.begin() + 2, str2.begin(), str2.end());
cout << str1;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
输出结果为:abefgcd
(5)erase()
str.erase();
有两种用法,删除单个元素,删除一个区间内的所有元素,时间复杂度均为O(N)
删除单个元素
str.erase(it);
用于删除单个元素,it
为需要删除的元素的迭代器
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcdefg";
str.erase(str.begin() + 3);
//删除str[3],即'd'
cout << str;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
输出结果为:abcefg
删除一个区间内的所有元素
str.erase(first, last);
,其中first
为需要删除的区间的起始迭代器,last
则为需要删除的区间的末尾迭代器的下一个地址,即删除[first, last)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcdefg";
str.erase(str.begin() + 3, str.end());
//删除str[3] ~ str[6]
cout << str;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
输出结果为:abc
str.erase(pos, length);
其中pos
为需要开始删除的起始位置,length
为删除的字符个数
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcdefg";
str.erase(3, 4);
//删除str[3] ~ str[6]
cout << str;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
输出结果为:abc
(6)clear()
str.clear();
用来清空string
中的数据,时间复杂度一般为O(1)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcdefg";
str.clear();
cout << str.size();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
输出结果为:0
(7)substr()
str.substr(pos, len);
用来返回从pos
号位开始,长度为len
的子串,时间复杂度为O(len)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abcdefg";
cout << str.substr(3, 4);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
输出结果为:defg
(8)string::npos
string::npos
是一个常数,其本身的值为-1
或者等于4294967295
,两个值都被认为是正确的
#include <iostream>
#include <string>
using namespace std;
int main()
{
if (string::npos == -1)
cout << "true" << endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
输出结果为:true
(9)find()
str.find(str2)
,当str2
是str
的子串时,返回其在str
中第一次出现的位置,如果str2
不是str
的子串,返回string::npos
str.find(str2, pos)
,从str
的pos
号位开始匹配str2
,返回值和上述相同
时间复杂度为O(nm),其中n和m分别是str
和str2
的长度
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "chen must like C++";
string str1 = "like";
string str2 = "C++";
string str3 = "not";
if (str.find(str1) != string::npos)
cout << str.find(str1) << endl;
if (str.find(str2) != string::npos)
cout << str.find(str2) << endl;
if (str.find(str3) != string::npos)
cout << str.find(str3) << endl;
else cout << "Wrong";
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
输出结果为:
10
15
Wrong
(10)replace()
str.replace(pos, len, str2);
把str
从pos
号位开始,长度为len
的子串替换为str2
str.replace(it1, it2, str2)
把str
的迭代器[it1, it2)
范围的子串替换为str2
时间复杂度为O(str.length())
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "chen must like C++";
string str2 = "algorithm";
string str3 = "math";
cout << str.replace(15, 3, str2) << endl;
cout << str.replace(str.begin() + 15, str.end(), str3);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
输出结果为:
chen must like algorithm
chen must like math
文章来源: chen-ac.blog.csdn.net,作者:辰chen,版权归原作者所有,如需转载,请联系作者。
原文链接:chen-ac.blog.csdn.net/article/details/116417774
- 点赞
- 收藏
- 关注作者
评论(0)