STL---string
string()定义
在 C 语言中,一般使用字符数组 char str[]来存放字符串,但是使用字符数组有时会显得操作麻烦,而且容易因经验不足而产生一些错误。为了使编程者可以更方便地对字符串进行操作,C++在 STL 中加入了string 类型,对字符串常用的需求功能进行了封装,使得操作起来更方便,且不易出错。
如果要使用 string,需要添加string头文件,即#include < string >(注意 string.h 和 string是不一样的头文件)。除此之外,还需要在头文件下面加上一句∶"using namespace std;",这样就可以在代码中使用string了。下面来看string的一些常用用法。
string的定义 定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可∶
string str;
如果要初始化,可以直接给 string类型的变量进行赋值∶
string str = "abcd";
string 中内容的访问
(1)通过下标访问 一般来说,可以直接像字符数组那样去访问string∶
#include<iostream>
#include<string>
using namespace std;
//string
int main()
{
string str = "abcd";
for(int i = 0; i < str.length(); i++)
{
cout << str[i] << endl;
}
return 0;
}
输出结果:
a
b
c
d
如果要读入和输出整个字符串,则只能用cin和cout:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin >> str;
cout << str;
return 0;
}
输出结果:任意输入都会输出同样的字符串。
也可使用c_str()将string类型转为字符数组进行输出:
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
//str.c_str
int main()
{
string str = "abcd";
printf("%s\n",str.c_str());
return 0;
}
输出结果:
abcd
(2)通过迭代器访问
一般仅通过(1)即可满足访问的要求,但有些函数比如insert()与erase()则要求以迭代器为参数:
由于string不像其他STL容器那样需要参数,因此可以直接如下定义:
string::iterator it;
这样就得到了迭代器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 << endl;
}
return 0;
}
a
b
c
d
string常用函数
(1)operator+=
operator+=,可以将两个string直接拼接起来
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "abc",str2 = "xyz",str3;
str3 = str1 + str2; //将str1和str2拼接,赋值给str3
str1 += str2; //将str2直接拼接到str1上
cout << str1 << endl;
cout << str3 << endl;
return 0;
}
输出结果:
abcxyz
abcxyz
(2)compare operator
两个string类型可以直接使用==、!=、<、<=、>、>=比较大小,比较规则是字典序
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "aa",str2 = "aaa",str3 = "abc",str4 = "xyz";
if(str1 < str2) cout << "ok1" << endl;
if(str1 != str3) cout << "ok2" << endl;
if(str4 >= str3) cout << "ok3" << endl;
return 0;
}
输出结果:
ok1
ok2
ok3
(3)length()/size()
length()返回string的长度,即存放的字符数,时间复杂度为O(1),size()与length()基本相同
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abdxyz";
cout << str.length() << " " << str.size() << endl;
return 0;
}
输出结果:
6 6
(4)insert()
string的insert()函数有很多种写法,这里给出几个常用的写法,时间复杂度为O(N)
1、insert(pos,string),在pos号位置插入字符串string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "abcxyz",str2 = "opq";
str1.insert(3,str2);
cout << str1 << endl;
return 0;
}
输出结果:
abcopqxyz
2、insert(it,it2,it3),it为要插入原字符串的位置,it2和it3为待插字符串的首位迭代器,用来表示串[it2,it3)将被插在it的位置上
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "abcxyz",str2 = "opq";
str1.insert(str1.begin() + 3,str2.begin(),str2.end());
cout << str1 << endl;
return 0;
}
输出结果:
abcopqxyz
(5)erase()
erase()有两种用法:删除单个元素、删除一个区间内的所有元素,时间复杂度均为O(N)
1、删除单个元素
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abcdefg";
str.erase(str.begin()+4);
cout << str << endl;
return 0;
}
输出结果:
abcdfg
2、删除一个区间内所有的元素,有两种方法:
2.1、str.erase(first,last),其中first为需要删除的区间的起始迭代器,而last则为需要删除的区间的末尾迭代器的下一个地址,也即为删除[first,last)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abcdefg";
//删除在[str.begin() + 2,str.end() - 1)内的元素,即cdef
str.erase(str.begin() + 2,str.end() - 1);
cout << str << endl;
return 0;
}
输出结果:
abg
2.2、str.erase(pos,length),其中pos为需要开始删除的起始位置,length为删除的字符个数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abcdefg";
str.erase(3,2);
cout << str << endl; //删除从3号位开始的2个字符,即de
return 0;
}
输出结果:
abcfg
(6)clear()
clear()用以清空string中的数据,时间复杂度一般为O(1)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abcd";
str.clear();
cout << str.length() << endl;
return 0;
}
输出结果:
0
(7)substr()
substr(pos,len),返回从pos号位置开始、长度为len的子串,时间复杂度为O(len)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "thank you for your smile.";
cout << str.substr(0,5) << endl;
cout << str.substr(14,4) << endl;
cout << str.substr(19,5) << endl;
return 0;
}
输出结果:
thank
your
smile
(8)string::npos()
string::npos是一个常数,其本身的值为-1,但由于是unsigned_int类型,因此实际上也可以认为是unsigned_int类型的最大值。string::npos用以作为find函数失配时的返回值。
例如在下面的实例中可以认为string::npos等于-1或者4294967295
#include<iostream>
#include<string>
using namespace std;
int main()
{
if(string::npos == -1)
{
cout << "-1 is true." << endl;
}
if(string::npos == 4294967295)
{
cout << "4294967295 is also true." << endl;
}
return 0;
}
输出结果:
"-1 is true.
4294967295 is also true.
(注意:不同PC可以值不一样,可通过以下代码验证,我的PC上string::npos最大值为 18446744073709551615)
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
size_t npos = -1;
cout << "npos: " << npos << endl;
cout << "size_t max: " << numeric_limits<size_t>::max() << endl;
}
输出结果:
npos: 18446744073709551615
size_t max: 18446744073709551615
(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 = "thank you for your smile.";
string str2 = "you";
string str3 = "me";
if(str.find(str2) != string::npos)
{
cout << str.find(str2) << endl;
}
if(str.find(str2,7) != string::npos)
{
cout << str.find(str2,7) << endl;
}
if(str.find(str3) != string::npos)
{
cout << str.find(str3) << endl;
}
else
{
cout << "I know there is no position for me." << endl;
}
return 0;
}
输出结果:
6
14
I know there is no position for me.
(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 = "Maybe you will turn around.";
string str2 = "will not";
string str3 = "surely";
cout << str.replace(10,4,str2) << endl;
cout << str.replace(str.begin(),str.begin() + 5,str3) << endl;
return 0;
}
输出结果:
Maybe you will not turn around.
surely you will not turn around.
本文详细介绍了C++ STL中的string类,包括string的定义、内容访问方式以及十大常用函数:operator+=、compare、length/size、insert、erase、clear、substr、string::npos、find和replace。通过对这些函数的讲解,帮助读者全面理解和掌握如何高效地使用C++中的string。

802

被折叠的 条评论
为什么被折叠?



