C++ STL 之 string
- #include <iostream>
- #include <string>
- using namespace std;
- // 初始化
- void test01()
- {
- string s1; // 调用无参构造 创建一个空的字符串
- string s2(, 'c'); // 使用 n 个字符 c 初始化
- string s3("abcdefg"); // 使用字符串初始化
- string s4(s3); // 使用一个 string 对象初始化另一个 string 对象(拷贝构造)
- cout << s1 << endl;
- cout << s2 << endl;
- cout << s3 << endl;
- cout << s4 << endl;
- cout << "-----------------" << endl;
- }
- // 赋值操作
- // string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
- // string& operator=(const string &s);//把字符串 s 赋给当前的字符串
- // string& operator=(char c);//字符赋值给当前的字符串
- // string& assign(const char *s);//把字符串 s 赋给当前的字符串
- // string& assign(const char *s, int n);//把字符串 s 的前 n 个字符赋给当前的字符串
- // string& assign(const string &s);//把字符串 s 赋给当前字符串
- // string& assign(int n, char c);//用 n 个字符 c 赋给当前字符串
- // string& assign(const string &s, int start, int n);//将 s 从 start 开始 n 个字符赋值给字符串
- void test02()
- {
- string s1;
- string s2("appp");
- s1 = "abcdef";
- cout << s1 << endl;
- s1 = s2;
- cout << s1 << endl;
- s1 = 'a';
- cout << s1 << endl;
- // 成员方法assign
- s1.assign("jkl");
- cout << s1 << endl;
- cout << "-----------------" << endl;
- }
- // 取值操作
- void test03()
- {
- string s1 = "abcdefg";
- // 重载[]操作符
- for (int i = ; i < s1.size(); i++)
- {
- cout << s1[i] << " ";
- }
- cout << endl;
- // at成员函数
- for (int i = ; i < s1.size(); i++)
- {
- cout << s1.at(i) << " ";
- }
- cout << endl;
- try
- {
- // cout << s1[100] << endl;
- cout << s1.at() << endl;
- }
- catch (...)
- {
- cout << "越界" << endl;
- }
- cout << "-----------------" << endl;
- // 区别:[]方式如果访问越界,就直接挂了
- // at方式 访问越界会抛出异常out_of_range
- }
- // 拼接操作
- // string& operator+=(const string& str);//重载+=操作符
- // string& operator+=(const char* str);//重载+=操作符
- // string& operator+=(const char c);//重载+=操作符
- // string& append(const char *s);//把字符串 s 连接到当前字符串结尾
- // string& append(const char *s, int n);//把字符串 s 的前 n 个字符连接到当前字符串结尾
- // string& append(const string &s);//同 operator+=()
- // string& append(const string &s, int pos, int n);//把字符串 s 中从 pos 开始的 n 个字符连接到当前字符串结尾
- // string& append(int n, char c);//在当前字符串结尾添加 n 个字符 c
- void test04()
- {
- string s = "abcd";
- string s2 = "";
- s += "abcd";
- s += s2;
- cout << s << endl;
- string s3 = "";
- s2.append(s3);
- cout << s2 << endl;
- string s4 = s2 + s3;
- cout << s4 << endl;
- cout << "-----------------" << endl;
- }
- // 查找操作
- // int find(const string& str, int pos = 0) const; //查找 str 第一次出现位置,从 pos 开始查找
- // int find(const char* s, int pos = 0) const; //查找 s 第一次出现位置,从 pos 开始查找
- // int find(const char* s, int pos, int n) const; //从 pos 位置查找 s 的前 n 个字符第一次位置
- // int find(const char c, int pos = 0) const; //查找字符 c 第一次出现位置
- // int rfind(const string& str, int pos = npos) const;//查找 str 最后一次位置,从 pos 开始查找
- // int rfind(const char* s, int pos = npos) const;//查找 s 最后一次出现位置,从 pos 开始查找
- // int rfind(const char* s, int pos, int n) const;//从 pos 查找 s 的前 n 个字符最后一次位置
- // int rfind(const char c, int pos = 0) const; //查找字符 c 最后一次出现位置
- void test05()
- {
- string s = "abcdefgffffghijkl";
- // 查找第一次出现的位置
- int pos = s.find("fg");
- cout << "pos: " << pos << endl;
- // 查找最后一次出现的位置
- pos = s.rfind("fg");
- cout << "pos: " << pos << endl;
- cout << "-----------------" << endl;
- }
- // string替换
- // string& replace(int pos, int n, const string& str); //替换从 pos 开始 n 个字符为字符串 str
- // string& replace(int pos, int n, const char* s); //替换从 pos 开始的 n 个字符为字符串 s
- void test06()
- {
- string s = "abcdefg";
- s.replace(, , "");
- cout << s << endl;
- cout << "-----------------" << endl;
- }
- // string 比较
- /* compare 函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的 A 比小写的 a 小。 */
- // int compare(const string &s) const; //与字符串 s 比较
- // int compare(const char *s) const;//与字符串 s 比较
- void test07()
- {
- string s1 = "abcd";
- string s2 = "abce";
- if (s1.compare(s2) == )
- {
- cout << "字符串相等!" << endl;
- }
- else
- {
- cout << "字符串不相等!" << endl;
- }
- cout << "-----------------" << endl;
- }
- // 子串操作
- // string substr(int pos = 0, int n = npos) const;//返回由 pos 开始的 n 个字符组成的字符串
- void test08()
- {
- string s = "abcdefg";
- string mySubstr = s.substr(, );
- cout << mySubstr << endl;
- cout << "-----------------" << endl;
- }
- // 插入和删除
- // string& insert(int pos, const char* s); //插入字符串
- // string& insert(int pos, const string& str); //插入字符串
- // string& insert(int pos, int n, char c);//在指定位置插入 n 个字符c
- // string& erase(int pos, int n = npos);//删除从 Pos 开始的 n 个字符
- void test09()
- {
- string s = "abcdefg";
- s.insert(, "");
- cout << s << endl;
- s.erase(, );
- cout << s << endl;
- }
- int main()
- {
- test01();
- test02();
- test03();
- test04();
- test05();
- test06();
- test07();
- test08();
- test09();
- getchar();
- return ;
- }
- //string 转 char*
- string str = "itcast";
- const char* cstr = str.c_str();
- //char* 转 string
- char* s = "itcast";
- string sstr(s);
C++ STL 之 string的更多相关文章
- STL的string和wstring
STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...
- C++之STL之string
/*C 语言中字符数组一般会采用char str[]来存放,但是显得会比较麻烦,C++在stl中加入了string类型,对字符串常用的功能进行了封装,操作起来比较方便*/#include<cst ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- C++STL之string (转)
在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串 string ...
- C++STL之String
本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...
- STL 的string类怎么啦?
前言 上个周末在和我的同学爬香山闲聊时,同学说到STL中的string类曾经让他备受折磨,几年前他开发一个系统前对string类还比较清楚,然后随着程序的复杂度的加深,到了后期,他几乎对strin ...
- STL之string使用简介
声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...
- STL:string类中size()与length()的区别
结论是:两者没有任何区别 解释: C++Reference中对于两者的解释: 两者的具体解释都一模一样: 理解: length是因为C语言的习惯而保留下来的,string类最初只有length,引进S ...
- C++ STL介绍——String类
目录 1.简介 2.string类成员函数汇总 3.String类的构造函数以及析构函数 4.获取字符串长度 5.获取字符串元素 6.字符串比较方法 7.字符串输入输出 8.字符串查找函数 1.简介 ...
- 【STL】string 常用函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
随机推荐
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_6.RabbitMQ研究-入门程序-消费者
我们在consumer这个功能下进行代码的编写 首先是新建这个层级的包 创建入门程序的消费者 消费者也需要和mq建立通道.建立连接创建通道 在顶部都声明这个队列 下面写核心代码监听队列.basicCo ...
- 获取当前运行的exe路径
void GetAppPath(CString& path) { TCHAR str[] = {}; GetModuleFileName(NULL,str,); wchar_t *pszPos ...
- Qt开发经验小技巧合集
一.开发经验总结 当编译发现大量错误的时候,从第一个看起,一个一个的解决,不要急着去看下一个错误,往往后面的错误都是由于前面的错误引起的,第一个解决后很可能都解决了. 定时器是个好东西,学会好使用它, ...
- es6 是否包含字符串判断
字符串查找类 接下来介绍一些可以通过 ES5 PolyFill的方法,但是现在 ES6 原生实现了 Method Param Return Description includes() 需要验证是否被 ...
- swift 第十三课 GCD 的介绍和使用
手头的项目中基本没有用到这个,但是还是要探索下.毕竟好多的地方要用这个,而且现在手机和电脑等电子设备都是多核心的,这样就成就了多线程带来更加优越的用户体验. 先记录下,自己看到的两个不错的连接: ht ...
- MySQL创建用户、授权、删除
1.在MySQL中创建新用户 使用具有shell访问权限的root用户登录MySQL服务器并创建名为“rahul”的新用户.下面的命令只允许从localhost系统访问用户rahul的MySQL服务器 ...
- 【计算机视觉】深度相机(四)--Realsense概览
本文参考下文,做一点个人使用补充. http://blog.csdn.net/app_12062011/article/details/52662143 转自: http://blog.csdn. ...
- 【Matlab开发】matlab中bar绘图设置与各种距离度量
[Matlab开发]matlab中bar绘图设置与各种距离度量 标签(空格分隔): [Matlab开发] [机器学习] 声明:引用请注明出处http://blog.csdn.net/lg1259156 ...
- 学习笔记:CentOS7学习之十五: RAID磁盘阵列的原理与搭建
目录 学习笔记:CentOS7学习之十五: RAID磁盘阵列的原理与搭建 14.1 RAID概念 14.1.1 RAID几种常见的类型 14.1.2 RAID-0工作原理 14.1.3 RAID-1工 ...
- C#中异步编程异常的处理方式
异步编程异常处理 在同步编程中,一旦出现错误就会抛出异常,我们可以使用try-catch来捕捉异常,未被捕获的异常则会不断向上传递,形成一个简单而统一的错误处理机制.但是对于异步编程来说,异常处理一直 ...