String类的常用接口
1.构造和析构
1.string()
// 构造空的string类对象
2.string(const char* s)
// 用C-string来构造string类对象
3.string(size_t n,char c)
// string类对象包含n个字符c
4.string(const string&s)
// 拷贝构造
5.string(const string&s,size_t pos,size_t n)
// 从pos位置开始拷贝n个字符构造string类对象
6.string(const char* s,size_t n)
// 用C-string里的前n个字符构造string类对象
void TestString1()
{
string s1;
string s2("hello");
string s3(7, 'a');
string s4(s2);
string s5(s2, 0, 3);
string s6("world", 4);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
}
2.迭代器
1.begin
//将迭代器返回到开头(公共成员函数)
2.end
//返回迭代器到结束(公共成员函数)
3.rbegin
//返回反向迭代器以反向开始(公共成员函数)
4.rend
//将反向迭代器返回到反向端点
void TestString2()
{
//begin()/end()
string s1("I am fine");
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it;
++it;
}
cout << endl;
//rbegin()/rend()
string s2("Bad gun!");
string::reverse_iterator rit = s2.rbegin();
while (rit != s2.rend())
{
cout << *rit;
++rit;
}
cout << endl;
}
3.容量
1.size
// 返回字符串有效字符长度
2.length
// 返回字符串有效字符长度
3.capacity
// 返回空间总大小
4.empty
// 检测字符串是否为空串,是返回true,否则返回false
5.clear
// 清空有效字符,不改变底层空间大小
6.void reserve(size_t n=0);
// 扩容,将底层空间容量修改到n,不改变有效元素个数
7.resize(size_t n) ;
// 将有效字符个数改成n个,多出的空间用字符’\0’填充
resize(size_t n,char c);
// 将有效字符个数改成n个,多出的空间用字符c填充
// 注:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量大小;如果时元素个数减少,底层空间大小不变
void TestString3()
{
string s("Iamfine");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s.empty() << endl;
s.reserve(20);
cout << s.capacity() << endl;
s.resize(10, '!');
cout << s << endl;
s.clear();
cout << s << endl;
}
- 测试扩容
void TestPushBack()
{
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";
}
}
}
void TestPushBackReserve()
{
string s;
s.reserve(100);
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';
}
}
}
结论:当reserve参数小于string的底层空间大小时,reserve不会改变容量大小;当reserve参数大于string的底层空间大小时,有自己的扩容机制(vs:1.5倍,linux:2倍)
4.string类对象的访问及遍历
1.char& operator[](size_t pos);
const char& operator[](size_t pos) const;
// 返回pos位置的字符,const string类对象调用
2.迭代器
3.范围for
4.char& at(size_t pos);
const char& at (size_t pos) const;
// 返回从pos位置开始的引用
5.char& front();
const char& front() const;
// 返回第一个有效字符的引用
6.char& back();
const char& back() const;
// 返回最后一个有效字符的引用
void TestString4()
{
string s("I am fine");
//1.for+operator[]
for (int i = 0; i < s.size(); i++)
{
cout << s[i];
}
cout << endl;
//2.迭代器
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it;
++it;
}
cout << endl;
string::reverse_iterator rit = s.rbegin();
while (rit != s.rend())
{
cout << *rit;
++rit;
}
cout << endl;
//3.范围for
for (auto ch : s)
{
cout << ch ;
}
cout << endl;
//4.at
for (int i = 0; i<s.size(); ++i)
{
std::cout << s.at(i);
}
cout << endl;
//5.front
string s1("test string");
s1.front();
cout << s1 << '\n';
//6.back
string s2("test string");
s2.back();
cout << s2 << '\n';
}
5.修改的操作
1.push_back
// 在字符串后尾插字符c
2.append
// 在字符串后尾插字符串
3.operator+=
// 在字符串后追加字符串str
4.c_str
// 返回c格式字符串
5.size_t find (const char* s, size_t pos, size_t n) const;
// 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
6.rfind
// 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
7.substr
// 在字符串中从pos位置开始,截取n个字符,将其返回
8.string& erase(size_t pos = 0, size_t len = npos);
// 从pos位置开始删除len个字符
void TestString5()
{
string s;
s.push_back('I');
s.push_back(' ');
s.append("am");
s.push_back(' ');
s += 'f';
s += "ine";
cout << s << endl;
cout << s.c_str() << endl;
size_t pos = s.rfind('f');
string str(s.substr(pos, s.size() - pos));
cout << str << endl;
string s1("Helloworld");
s1.erase(3, 1);
cout << s1 << endl;
string s2("http://www.baidu.com");
size_t p = s2.find("www");
if (p != string::npos)
{
string ret = s2.substr(p);
cout << ret << endl;
}
}
6.特殊操作
1.getline
// 获取一行字符串
2.relational operators
// 大小比较
3.opreator>>
4.opreator<<
5.operator+
void TestString6()
{
string s;
// 一次接收一个
while (cin >> s)
{
cout << s << endl;
}
}
void TestString7()
{
char str[100];
gets(str);
string s;
// 循环接收一行字符串
while (getline(cin, s))
{
cin >> s;
cout << s.substr(s.rfind(' ') + 1).size() << endl;
}
}
void TestString8()
{
string s1("hello");
string s2("world");
if (s1 > s2)
{
cout << s1 << endl;
}
else
{
cout << s2 << endl;
}
}