int main()
{
string s1;//无参构造初始化为空字符串
string s2("abcdefg");//用C-string来构造string类对象
string s3(s2);//拷贝构造函数
string s4(5, '*');//string类对象中包含n个字符c
return 0;
}
作用:返回字符串的有效字符长度
string s6 = "abcdefg";
int size = s6.size();//返回7
作用:检测字符串是否为空串,是返回true,否则返回false
string s7;
string s8 = "abcdef";
s7.empty();//返回true
s8.empty();//返回false
作用:返回空间总大小
在VS2019中默认给string对象开辟15字节,并且以原容量的二倍进行扩容
string s;
cout << s.capacity() << endl;//输出15
s += "abcdefghijklmnop";
cout << s.capacity() << endl;//输出31
在vim中有多少字符空间就有多大
string s;
cout << s.capacity() << endl;//输出0
s += "abcdefghijklmnop";
cout << s.capacity() << endl;//输出16
作用:为字符串预留空间
注意区分:reserve是保留,预留 reverse是翻转
在VS2019中
string s1 = "abc";
cout << s1.capacity() << endl;//输出15
s1.reserve(100);
cout << s1.capacity() << endl;//输出111
在vim中
string s1 = "abc";
cout << s1.capacity() << endl;//输出3
s1.reserve(100);
cout << s1.capacity() << endl;//输出100
作用:清空有效字符
注意:clear()函数没有返回值
VS2019中
string s9 = "abcdef";
cout << s9.capacity() << endl;//返回15
cout<<s9.empty()<<endl;//返回false
s9.clear();//清空字符串
cout << s9.capacity() << endl;//返回15
cout << s9.empty() << endl;//返回true
vim中
string s9 = "abcdef";
cout << s9.capacity() << endl;//返回6
cout<<s9.empty()<<endl;//返回false
s9.clear();//清空字符串
cout << s9.capacity() << endl;//返回6
cout << s9.empty() << endl;//返回true
可以看到在清除字符后字符串的原有容量并不会减小,size会变为0
作用:调整大小,将有效字符的个数改成n个,如果有多出的空间用字符c填充
string s3 = "abcdefg";
s3.resize(4);//缩小
cout << s3 << endl;//输出abcde
string s4 = "abcde";
s4.resize(10, 'd');//扩大
cout << s4 << endl;//输出abcdeddddd//多出来5个字节用字符‘d’来进行填充
//扩大时默认使用0来进行填充
string s4 = "abcdefg";
for (int i = 0; i < s4.size(); i++)
{
cout << s4[i] << " ";
}
cout << endl;
string s5 = "abcdefg";
string::iterator it;
for (it = s5.begin(); it != s5.end(); it++)
{
cout << *it << " ";
}//输出 a b c d e f g
cout << endl;
string s6 = "abcdefg";
string::reverse_iterator rit;//反向迭代器
for (rit = s6.rbegin(); rit != s6.rend(); rit++)
{
cout << *rit << " ";
}//输出g f e d c b a
cout << endl;
string s7 = "abcdefg";
for (const auto& e : s7)
{
cout << e << " ";
}
作用:在字符串后面追加字符
string s1 = "abcdefg";
s1 += "fff";
cout << s1 << endl; //输出abcdefgfff
string s2;
s2 = s1 + "hhh";
cout << s2 << endl;//输出abcdefgfffhhh
string s3 = s1 + s2;
cout << s3 <<end;//输出abcdefgfffabcdefgfffhhh
作用:把const string类型转换成const char类型,并返回const char*的指针
const char* ptr;
string s7 = "abcdefg";
ptr = s7.c_str();
cout << ptr << endl;//输出abcdefg
s7 = "aaaaaaaa";//改变原来的字符串
cout << ptr << endl;//输出aaaaaaaa
由此可见返回的指针和字符串指针,这两个指针是指向的同一个地址
作用:从pos位置开始向末尾位置查找字符串或者字符,并返回对应的下标,如果没找到则返回npos.
参数s或c表示要查找的字符串或者字符
pos指在字符串中开始查找的起始位置
n表示要查找的字符串长度
string s1 = "abcdeeeeefg";
cout << s1.find('a');//输出0
cout << s1.find("cde");//输出2
cout << s1.find("cde", 3);
//输出npos,从下标为3的位置开始找,没找到返回npos = 4294967295
作用:从pos位置开始向0位置查找字符串或者字符,并返回对应的下标,如果没找到则返回npos
string s2 = "abcdeeeeefg";
cout << s2.rfind('e') << endl;//输出8----rfind()是倒着查找的
cout << s2.rfind("cde") << endl;//输出2
cout << s2.rfind('m') << endl;//输出npos = 4294967295
string s3 = "abcdefgh";
string s4 = s3.substr(2, 5);
cout << s4 << endl;//输出cdefg
string s5 = s3.substr(2);
cout << s5 << endl;//输出cdefgh
npos是静态成员常量,值为static const size_t npos = -1;
-1对应的无符号整数为4294967295
string s6;
getline(cin, s6);//输入a b c gg----g后面也输入了两个空格
cout << s6 << endl;//输出a b c gg