我们都知道,在C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库还提供一些库函数,供我们使用,但是这些库函数与字符串是分离的,而且底层空间也需要用户自己管理,较为麻烦。
所以C++在标准库中提供了string类,大大方便了我们对字符串的操作。接下来我们就来看看string类的常见接口及使用方法。
1.string类对象的常见构造
函数名 | 功能 |
---|
string() | 构造空的string类对象,即空字符串 |
string(const char* s) | 用C-string来构造string类对象 |
string(size_t n, char c) | string类对象中包含n个字符c |
string(const string&s) | 拷贝构造函数 |
string(const string&s, size_t n) | 将s中的前n个字符除去,用剩下的字符构造新的string类对象 |
void TestString()
{
string s1;
string s2("C string");
string s3(10, 'c');
string s4(s3);
string s5(s2, 5);
string s6(s2, 2);
}
2.string类对象的容量操作
函数名 | 功能 |
---|
size_t size() const | 返回字符串有效字符长度 |
size_t length() const | 返回字符串有效字符长度 |
size_t capacity ( ) const | 返回空间总大小 |
bool empty ( ) const | 检测字符串释放为空串,是返回true,否则返回false |
void clear() | 清空有效字符 |
void resize ( size_t n, char c ) | 将有效字符的个数改成n个,多出的空间用字符c填充 |
void resize ( size_t n ) | 将有效字符的个数改成n个,多出的空间用0填充 |
void reserve ( size_t res_arg=0 ) | 为字符串预留空间 |
void TestString1()
{
string s("hello world!");
cout << s.length();
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(10, 'a');
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
void TestString2()
{
string s;
s.reserve(100);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(200);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
通过测试可以看出:
- 1.size()和length()底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,
- 2.clear()只是将string中有效字符清空,不改变底层空间大小,
- 3.resize 在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变,
- 4.reserve:为string预留空间,不改变有效元素个数,当其参数小于string的底层空间总大小时,reserve不会改变容量大小。
3.string类对象的访问操作
函数名 | 功能 |
---|
char& operator[](size_t pos) | 返回pos位置的字符,非const string类对象调用 |
const char& operator[](size_t pos) const | 返回pos位置的字符,const string类对象调用 |
void TestString()
{
string s1("hello world!");
const string s2("hello world!");
cout << s1 << " " << s2 << endl;
cout << s1[0] << " " << s2[0] << endl;
s1[0] = 'H';
cout << s1 << endl;
for (size_t i = 0; i < s1.size(); ++i)
cout << s1[i] << endl;
}
4.string类对象的修改操作
函数名 | 功能 |
---|
void push_back(char c) | 尾插字符c |
string& append(const char* s) | 追加一个字符串 |
string& operator+=(const string& str) | 追加字符串str |
string& operator+=(const char* s) | 追加C风格字符串 |
string& operator+=(char c) | 追加字符c |
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) | 从pos位置开始往前找字符c,返回该字符在字符串中的位置 |
string substr(size_t pos = 0, size_t n = npos) const | 从pos位置开始,截取n个字符,然后将其返回 |
void TestString()
{
string str;
str.push_back('h');
str.append("ello");
str += ' ';
str += "world!";
cout << str << endl;
cout << str.c_str() << endl;
string file("string.cpp");
size_t pos = file.find('.');
string suffix(file.substr(pos, file.size() - pos));
cout << suffix << endl;
string ur1("http://www.cplusplus.com/reference/string/string/find/");
cout << ur1 << endl;
size_t start = ur1.find("://");
if (start == string::npos)
{
cout << "invalid ur1" << endl;
return;
}
start += 3;
size_t finish = ur1.find('/', start);
string address = ur1.substr(start, finish - start);
cout << address << endl;
pos = ur1.find("://");
ur1.erase(0, pos + 3);
cout << ur1 << endl;
}
void TestPushBack()
{
string s;
size_t sz = s.capacity();
cout << "making s 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()
{
string s;
s.reserve(100);
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s += 'c';
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
通过测试,我们可以总结出:
- 1.在string尾部追加字符时,s.push_back( c ) /s.append(1, c)/s += 'c’三种方式相差不大,一般情况下+=操作用的较多,因为+=操作不仅可以连接单个字符,还可以连接字符串,
- 2.对string操作时,如果能够大概预估到放多少字符,可以先通过reverse把空间预留好。这样可以避免增容带来的开销,提高插入数据的效率。