马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
目次
一.标准库中的string类
二.string的常用接口函数
2.1string类对象的构造
2.2 string的容量操纵
2.3 string类的访问与遍历
2.4 string类对象的修改
2.5 string类常用的非成员函数
三、总结
一.标准库中的string类
可以简单明确成把string类明确为变长的字符数组,我们可以对它举行增删查改等一系列操纵,同时有一些列封装的接口函数提供给我们可以让我们直接利用。
二.string的常用接口函数
2.1string类对象的构造
- string s2("hello bit"); // 用C格式字符串构造string类对象s2
- cout << s2 << endl;
- cout << endl;
- string s3(s2); // 拷贝构造s3string s3(s2); // 拷贝构造s3
- cout << s3 << endl;
复制代码 我们普便利用以上的两种方法来构造string。
2.2 string的容量操纵
- int main()
- {
- string s1("abcdef");
- cout << "s1:" << s1 << endl;
- cout << "size:" << s1.size() << endl; //有效字符的个数
- cout << "length:" << s1.length() << endl; //有效字符的个数
- //上面这两个功能上差别不大,一般我们用size()用的多一点
- cout << "capacity:" << s1.capacity() << endl;
- //开辟的空间大小(当空间不够时会自动扩容,扩容空间为原空间的1.5倍(与环境有关))
- cout << "empty:" << s1.empty() << endl; //检查字符串是否为空,0表示非空,1表示空
- s1.clear(); //清空字符串
- cout << "s1:" << s1 << endl;
- s1.reserve(50); //开辟指定大小空间(一般会多一点)
- cout << "capacity:" << s1.capacity() << endl;
- s1.resize(5, 'a');
- cout << "size:" << s1.size() << endl;
- cout << "s1:" << s1 << endl;
- return 0;
- }
复制代码
2.3 string类的访问与遍历
- int main()
- {
- string s1("abcdef");
-
- //访问方法:下标访问法
- cout << s1[1] << endl;
- cout << s1[2] << endl;
- s1[0] = 'h';
-
-
- cout << "下标遍历法:";
- for (int i = 0; i < s1.size(); i++)
- {
- cout << s1[i] << " ";
- }
- cout << endl;
-
-
- cout << "迭代器法(正向):";
- string::iterator it = s1.begin();
- for (; it != s1.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
-
-
- cout << "迭代器(反向):";
- string::reverse_iterator rit = s1.rbegin();
- while (rit != s1.rend())
- {
- cout << *rit << " ";
- rit++;
- }
- cout << endl;
-
-
- cout << "范围for法:";
- for (auto e : s1)
- {
- cout << e << " ";
- }
- cout << endl;
-
- return 0;
- }
复制代码
2.4 string类对象的修改
- int main()
- {
- string s1("sssssssss");
- cout << s1 << endl;
-
- //push_back 在末尾加入字符
- cout << "push_back后:";
- s1.push_back('w');
- cout << s1 << endl;
-
- //append 在末端加入字符串
- cout << "append后:" ;
- s1.append(" www");
- cout << s1 << endl;
-
- //operator+= 在末端随意添加
- cout << "+=后:";
- s1 += " yy";
- cout << s1 << endl;
-
- //c_str 返回C格式字符串
- cout << "c_str:";
- const char* m = s1.c_str();
- cout << m << endl;
-
- //find 从pos位置开始查找字符并返回其位置
- cout << "find:";
- int npos1 = s1.find('y');
- cout << npos1 << endl;
-
- //rfind 从pos位置开始往前查找字符并返回其位置
- cout << "rfind:";
- int npos2 = s1.rfind('y');
- cout << npos2 << endl;
-
- //substr 从pos位置开始截取n个字符并返回
- cout << "substr后:";
- string tmp = s1.substr(npos1, npos2 - npos1);
- cout << tmp << endl;
-
- return 0;
- }
复制代码
2.5 string类常用的非成员函数
- int main()
- {
- string s1("hello ");
- string s2("bit");
-
- //operator+ 涉及深层拷贝,不建议多用
- cout << "operator+后:";
- cout << operator+(s1, s2) << endl;
-
- //operator>> 输入运算符重载
- cout << "operator>>:";
- string s3;
- operator>>(cin,s3);
- cout << s3 << endl;
-
- //operator<< 输出运算符重载
- cout << "operator<<:";
- operator<<(cout, s1) << endl;
-
-
- return 0;
- }
复制代码
三、总结
以上就是常见的string封装的接口函数,下期将解说关于怎样实现这些函数。
请大佬们一键三连~
|