String类的常用接口说明–C++
string是表示字符串的字符串类,该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
在使用string类时,必须包含头文件以及using namespace std;
- string类型的常见构造
函数名称 | 功能说明 |
---|---|
string () | 构造空的string类字符串,即空字符串 |
string (const char& str) | 用C–string来构造string类对象 |
string(size_t n, char c) | string类里面包含n个字符c |
string (const string& str) | 拷贝构造函数 |
string(const string& str,size_t n) | 用str中的前n个字符构造新的string类对象 |
代码:
void TestString1()
{
string s1;//构造空的string类对象s1
string s2("hello world");//用C格式字符串来构造string类对象s2
string s3(5, 't');//用5个字符‘t’来构造string类对象s3
string s4(s3);//拷贝构造s4
string s5(s2, 4);//用s2的前4个字符来构造string类对象s5
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
}
2.string类对象的容量操作(capacity)
函数名称 | 功能说明 |
---|---|
size_t size( )const | 返回字符串有效字符的长度 |
size_t length()const | 返回字符串的有效长度 |
size_t capacity()const | 返回总空间大小 |
bool empty()const | 检测字符串是否为空串,是返回true,否则返回false |
vois clear() | 清空有效字符 |
void resize(size_t n, char c) | 将有效字符的个数改成n个,多于空出来的用字符c来填充 |
void resize(size_t n) | 将有效字符的个数改成n个,多于空出来的用字符0来填充 |
void reserve(size_t n = 0) | 为字符串预留空间 |
注意:
(1)String类函数中,size()与length()除了函数名不一样,其他的全部相同
(2)clear()函数不改变容量,但是字符串的有效字符长度为0
(3)resize(),如果n<_size,则容量不变;若n>_size,且n>_capacity,则需要扩容,n<_capacity,则不变
(4)reserve():为string预留空间,不改变有效元素的个数,当reserve参数小于string底层空间的总大小时,reserve不会改变容量的大小
代码:
void TestString2()
{
string s("hello,world!");
cout << s.size() << endl;
cout << s.length() << endl;//12
cout << s.capacity() << endl;//15
cout << s.empty() << endl;
cout << s << endl;
cout << " "<< endl;
//清空s里的字符串
s.clear();
cout << s.size() << endl;
cout << s.length() << endl;//0
cout << s.capacity() << endl;//15
cout << s << endl;
cout << " " << endl;
//将S中的有效字符个数增大到18个,多出的字符用‘t’来填充
s.resize(18, 't');
cout << s.size() << endl;
cout << s.length() << endl;//18
cout << s.capacity() << endl;//31(成倍的扩容)
cout << s << endl;
cout << " " << endl;
//将S中的有效字符个数增大到19个,多出的字符用0来填充
s.resize(19);
cout << s.size() << endl;
cout << s.length() << endl;//18
cout << s.capacity() << endl;//31(成倍的扩容)
cout << s << endl;
cout << " " << endl;
//将s中的有效字符的个数缩小到6个
s.resize(5);
cout << s.size() << endl;
cout << s.length() << endl;//5
cout << s.capacity() << endl;//31
cout << s << endl;
cout << " " << endl;
}
void TestString3()
{
string s("I like it");
cout << s.size() << endl;//9
cout << s.capacity() << endl;//15
cout << " " << endl;
//reserve():为字符串预留空间
//增大
s.reserve(100);
cout << s.size() << endl;//9
cout << s.capacity() << endl;//111
cout << " " << endl;
//减小(不会改变容量大小)
s.reserve(10);
cout << s.size() << endl;//9
cout << s.capacity() << endl;//15
cout << " " << endl;
}
- string类对象的访问操作(access)
函数名 | 功能说明 |
---|---|
char& operator[] (size_t pos) | 返回pos位置的字符,const string类对象参与调用 |
const char& operator[] (size_t pos) const | 返回pos位置的字符,非const string类对象参与调用 |
代码:
void TestString4()
{
string s1("i like it");
const string s2("I like it");
cout << s1 << " " << s2 << endl;
cout << s1[0] << " " << s2[0] << endl;
cout << " " << endl;
s1[0] = 'I';//修改s1的第一位
cout << s1 << endl;
cout << " " << endl;
//s2[0] = 'o';//const类型不能修改
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << endl;
}
}
- strong类对象的修改操作(modify)
函数名称 | 功能说明 |
---|---|
void push_back (char c) | 在字符串后尾插字符c |
string& append (const char* s) | 在字符串后追加一个字符串 |
string& operator+= (const string& str) | string:在字符串后追加一个字符串str |
string& operator+= (const char* s) | C-string:在字符串后追加一个字符串 |
string& operator+= (char c) | charcter:在字符串后追加字符 |
代码:
void TestString5()
{
string s1("hello,world");
string s2(" me");
s1.push_back('!');//追加字符
s1.append("i like");//追加字符串
cout << s1 << endl;
s1 += 'y';
cout << s1 << endl;
s1 += "ou";
cout << s1 << endl;
cout << s1.c_str() << endl;//返回C格式字符串
s1 += s2;
cout << s1 << endl;
}
- String operations
函数名 | 功能说明 |
---|---|
const char* c_str() const | 返回C格式字符 |
size_t find (char c, size_t pos = 0) const | 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 |
size_t rfind (char c, size_t pos = npos) const | 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 |
string substr (size_t pos = 0, size_t len = npos) const | 在str中从pos位置开始,截取n个字符,然后将其返回 |
代码:
void TestString6()
{
//获取file1的后缀
string file1("string.cpp");
size_t pos = file1.rfind('.');
string s2(file1.substr(pos, file1.size() - pos));
cout << s2 << endl;
cout << " " << endl;
//取出str中的域名(www.cplusplus.com)
string str("http://www.cplusplus.com/reference/string/string/rfind/");
cout << str << endl;
size_t start = str.find("://")+3;
size_t finish = str.find('/', start);
string address = str.substr(start, finish - start);
cout << address << endl;
//删除str的前缀
pos = str.find("://");
str.erase(0, pos + 3);
cout << str << endl;
}
void TestPushBack()
{
//观察capacity每次系统自动扩容的大小(程度)
string s;
size_t sz = s.capacity();
cout << "making a grow:\n";
for (int i = 0; i < 100; i++)
{
s += 'c';
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed:" << sz << '\n';
}
}
}
void TestPushBack_P()
{
//利用reserve提高插入数据的效率,避免增容带来的开销
string s;
s.reserve(100);//预留100个字节的空间
size_t sz = s.capacity();
cout << "making a grow:\n";
for (int i = 0; i < 100; i++)
{
s += 'c';
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed:" << sz << '\n';
}
}
}