构造
1.无参构造
2.用C-string来构造(char*)
3.拷贝构造
4.重载操作符 =
5.string(size_t n, char c) string类对象中包含n个字符c
//无参构造
string str;
cout << str << endl;
cout << string("hello world") << endl;//匿名对象
//用C-string来构造string类对象
char*p = "hello world2";
string str1(p);
cout << str1 << endl;
//拷贝构造
string str2(str1);
cout << str2 << endl;
//重载操作符 =
string str3;
str3 = str2;
cout << str3 << endl;
//string类对象中包含n个字符c
string str4(10,'c');
string 类常用的成员函数
注意:
-
size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
-
clear()只是将string中有效字符清空,不改变底层空间大小。
-
resize(size_t n) 与 resize(size_t n, char
c)都是将字符串中有效字符个数改变到n个,没有指定char c是默认用 ‘a’来填充,resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。 -
reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
void TestString1()
{
// 注意:string类对象支持直接用cin和cout进行输入和输出
string s("hello, bit!!!");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
cout << "----------------" << endl;
// 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
//capacity 和 size 不一样 capacity类似公交车的大小 size相当于公交车的座位
//清空时 只是将座位清空 车大小不变
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << "----------------" << endl;
// 将s中有效字符个数增加到10个,多出位置用'a'进行填充 如果不手动设置填充元素 默认为‘a’
// “aaaaaaaaaa”
s.resize(10, 'a');
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << "----------------" << endl;
// 将s中有效字符个数增加到15个,多出位置用缺省值'a'进行填充
// 注意此时s中有效字符个数已经增加到15个
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
cout << "----------------" << endl;
// 将s中有效字符个数缩小到5个
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
用reserve 可以减少增容的开销
void TestPushBack()
{
//如果用reserve 可以减少增容的开销
string s;
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
string的三种遍历方式
- for + operator [ ]
- 迭代器
- c++11 范围for for ( auto n : s ) 想要修改 &n
for(size_t i = 0; i < s.size(); ++i)
cout<<s[i]<<endl;
// 2.迭代器
//支持随机访问 也可以it < s.end()
string::iterator it = s.begin();
while(it != s.end())
{
cout<<*it<<endl;
++it;
}
//反向迭代器
string::reverse_iterator rit = s.rbegin();
while(rit != s.rend())
cout<<*rit<<endl;
// 3.范围for
for(auto ch : s)
cout<<ch<<endl;
string类对象的修改操作
- 在string尾部追加字符时,s.push_back© / s.append(1, c) / s += 'c’三种的实现方式差不多,一般
情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。 - 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。
find
返回值是size_t类型 返回找到的下标
配合npos使用 当返回值和npos相等时 没找到
// npos是string里面的一个静态成员变量 static const size_t npos = -1;
string (1)
size_t find (const string& str, size_t pos = 0) const noexcept;
c-string (2)
size_t find (const char* s, size_t pos = 0) const;
buffer (3)
size_t find (const char* s, size_t pos, size_type n) const;
character (4)
size_t find (char c, size_t pos = 0) const noexcept;
substr
返回值为string 从下标 pos开始 共npos个有效字符
string substr (size_t pos = 0, size_t len = npos) const;
erase
1.从下标pos开始 删除npos个
2.删除迭代器对应元素 返回下一个元素迭代器
3.删除两迭代器中的所有元素 返回下一个元素迭代器
sequence (1)
string& erase (size_t pos = 0, size_t len = npos);
character (2)
iterator erase (const_iterator p);
range (3)
iterator erase (const_iterator first, const_iterator last);
string类非成员函数
string中重载了下列函数,具体用法可以查阅 cpluscplus.com