目录
预备知识
1.string成员函数
1.string()
2.string (const char* s);
3.string (size_t n, char c);
4.string (const string& str);(拷贝构造)
2.string类对象的容量操作
1.size == length
2.max_size
3.resize
4.capacity
5.empty
6.clear
7.reserve
8.resize
3.string类对象的访问及遍历操作
预备知识
创建一个string对象并初始化
int main(int argc, const char * argv[]) {
string s1 = "hello world";
cout << s1 <<endl;
string s2("hello world");
cout << s2 <<endl;
string s3(s2);
cout << s3 << endl;
// 不常用 了解
string s4(s2, 3, 5);
string s5(s2, 3);
string s6(s2, 3, 30);
string s7("hello world", 5);
string s8(10, 'x');
return 0;
}
1.string成员函数
1.string()
构造空的string类对象,即空字符串
2.string (const char* s);
int main(int argc, const char * argv[]) {
char s[] = "abcd";
string s1 = s;
cout << s1 << endl;
return 0;
}
输出结果:abcd
3.string (size_t n, char c);
int main(){
string s1(10,'x');
cout << s1 << endl;
return 0;
}
输出结果:xxxxxxxxxx
4.string (const string& str);(拷贝构造)
int main(){
string s1(10,'x');
//cout << s1 << endl;
string s2 = s1;//拷贝构造
cout << s2 << endl;
return 0;
}
输出结果:xxxxxxxxxx
2.string类对象的容量操作
1.size == length
返回字符串有效字符长度
int main(){
string s1(10,'x');
string s2 = s1;//拷贝构造
cout << s2 << endl;
cout << s2.size() <<endl;
cout << s2.length() <<endl;
return 0;
}
输出:
10
10
2.max_size
int main(){
string s1(10,'x');
string s2 = s1;//拷贝构造
cout << s2 << endl;
cout << s2.size() << endl;
cout << s2.length() <<endl;
cout << s2.max_size() << endl;
return 0;
}
输出:
xxxxxxxxxx
10
10
9223372036854775791//64位下
3.resize
将有效字符的个数成为n个,多出的空间用字符c填充
int main(){
string s1 = "Greatness,it is just something we made up.";
cout << s1 << endl;
cout << s1.size() << endl;
s1.resize(s1.size()+2, 'x');
cout << s1 << endl;
cout << s1.size() << endl;
s1.resize(20);
cout << s1 << endl;
cout << s1.size() << endl;
return 0;
}
输出结果:
Greatness,it is just something we made up.
42
Greatness,it is just something we made up.xx
44
Greatness,it is just
20
4.capacity
(返回空间总大小)
int main(){
string s1 = "Greatness,it is just something we made up.";
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() <<endl;
return 0;
}
//输出结果
Greatness,it is just something we made up.
42
47
5.empty
检测字符串是否为空串,是返回true,否则返回false
int main(){
string s1 = "Greatness,it is just something we made up.";
cout << s1.empty()<< endl;
cout << s1 <<endl;
string s2;
cout << s2.empty() <<endl;
return 0;
}
输出结果:
0
Greatness,it is just something we made up.
1
6.clear
清空有效字符
int main(){
string s1 = "Greatness,it is just something we made up.";
s1.clear();
cout << s1 <<endl;
return 0;
}
输出为空
7.reserve
为字符串预留空间,改变的是capacity
int main(){
string s1 = "Greatness,it is just something we made up.";
cout << s1.capacity() <<endl;
s1.reserve(100);
cout << s1.capacity() <<endl;
cout << s1 <<endl;
return 0;
}
输出结果:
47
111
Greatness,it is just something we made up.
8.resize
将有效字符的个数改为n个,多出的空间用字符c填充
int main(){
string s1 = "Greatness,it is just something we made up.";
cout << s1.size() << endl;
s1.resize(60, 'x');
cout << s1.size() << endl;
cout << s1 << endl;
return 0;
}
输出结果
42
60
Greatness,it is just something we made up.xxxxxxxxxxxxxxxxxx
注意:
1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一 致,一般情况下基本都是用size()。
2. clear()只是将string中有效字符清空,不改变底层空间大小。
3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的 元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大 小,如果是将元素个数减少,底层空间总大小不变。
4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于 string的底层空间总大小时,reserver不会改变容量大小。
3.string类对象的访问及遍历操作
int main(){
string s1("hello world");
// 遍历方式1:下标+[]
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
//遍历方式2: 迭代器
//string::iterator it1 = s1.begin();
auto it1 = s1.begin();
while (it1 != s1.end())
{
*it1 += 3;//'h' + 3 = 'k'
cout << *it1 << " ";
++it1;
}
cout << endl;
//遍历方式3: 范围for
// 底层角度,他就是迭代器
cout << s1 << endl;
for (auto& e : s1)
{
e++;
cout << e << " ";
}
cout << endl;
cout << s1 << endl;
return 0;
}
输出结果:
h e l l o w o r l d
k h o o r # z r u o g
khoor#zruog
l i p p s $ { s v p h
lipps${svph
void test_string5()
{
const string s1("hello world");
//string::const_iterator it1 = s1.begin();
auto it1 = s1.begin();
while (it1 != s1.end())
{
// 不能修改
//*it1 += 3;
cout << *it1 << " ";
++it1;
}
cout << endl;
//string::const_reverse_iterator cit1 = s1.rbegin();
auto cit1 = s1.rbegin();
while (cit1 != s1.rend())
{
// 不能修改
//*cit1 += 3;
cout << *cit1 << " ";
++cit1;
}
cout << endl;
string s2("hello world");
string::reverse_iterator it2 = s2.rbegin();
//auto it2 = s2.rbegin();
while (it2 != s2.rend())
{
//*it2 += 3;
cout << *it2 << " ";
++it2;
}
cout << endl;
}
int main(){
test_string5();
return 0;
}
输出结果:
h e l l o w o r l d
d l r o w o l l e h
d l r o w o l l e h