#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std; //1. 字符串构造
/*
string();//创建一个空的字符串 例如: string str;
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化 */
void test01(){ string s1;
string s2(10, 'a');
string s3(s2);
string s4("hello wolrd!"); cout << s2 << endl;//aaaaaaaaaa
cout << s3 << endl;//aaaaaaaaaa
cout << s4 << endl;//hello wolrd!
} //2. string基本赋值操作
/*
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;
s1 = "hello world!";
cout << s1 << endl;//hello world! string s2;
//s2.assign(s1);
s2.assign("hello obama!");
cout << s2 << endl;//hello obama!
} //3. string存取字符操作
/*
char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符
*/
void test03(){ string s = "hello world!"; for (int i = 0; i < s.size(); i ++){
cout << s[i] << " ";
} cout << endl;
for (int i = 0; i < s.size(); i++){
cout << s.at(i) << " ";
}
cout << endl; //[]访问越界不抛异常,直接挂掉。at越界了,会抛出异常。 try{
//cout << s[100] << endl;
cout << s.at(100) << endl;
}
catch (out_of_range &ex){
cout << ex.what() << endl;
cout << "捕获异常!" << endl;
} } //4. string拼接操作
/*
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 s1 = "aaa";
s1 += "bbb";
s1 += 'c';
cout << s1 << endl; s1.append("ddddd",3);
cout << s1 << endl;//aaabbbcddd } //5. string查找和替换
/*
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最后一次出现位置
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 test05(){
string s = "abcdefgd";
cout << "test05-1:"<< s.find('d') << endl;//4
cout << "test05-2:"<< s.rfind('d') << endl;//5 s.replace(2, 3, "AAA");
cout << "test05-3:"<<s << endl;//abAAAfgd
} //6. string比较操作
/*
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const;//与字符串s比较
*/
void test06(){
string s1 = "hello";
string s2 = "hello";
const char *str = "world"; if (s1.compare(s2) == 0){
cout << "s1 == s2!" << endl;
} if (s2.compare(str) == 0){
cout << "s1 == str!" << endl;
}
} //7. string子串
/*
string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
*/
void test07(){
string email = "hello world@gmail.com";
int pos = email.find('@');
string username = email.substr(0,pos);
cout << "test07-1:"<< username << endl;//hello world string prex = email.substr(pos + 1);
cout << "test07-2:"<< prex << endl;//gmail.com
} //8. string插入和删除操作
/*
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 test08(){
string s = "aaaa";
s.insert(3, "AAAA");
cout << "test08-1:"<< s << endl;//aaaAAAAa
s.insert(3, 5, 'M');
cout << "test08-2:"<< s << endl;//aaaMMMMMAAAAa
s.erase(2, 3);
cout << "test08-3:"<< s << endl;//aaMMMAAAAa
} //9. string和c-style字符串转换
void test09(){ const char *str = "hello wolrd!";
//const char * -> string
string s(str); cout << str << endl; //string -> const char *
const char *s2 = s.c_str();
cout << s2 << endl; } //10. 字符串的内存被重新分配之后,可能发生错误.
void test10(){ //[]和at函数返回都是引用
string s = "abcde";
char &a = s[2];
char &b = s[3]; a = '1';
b = '2'; cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << s << endl;
cout << "字符串原空间地址:" << (int *)s.c_str() << endl; s = "123456789abcdefg";
cout << "字符串原空间地址:" << (int *)s.c_str() << endl; //字符串在字符串操作过程中,可能会导致重新分配内存,之前引用的内存就会失效。
a = '3';
} //11. 迭代器遍历字符串
void test11(){ string s = "hello world!";
for (string::iterator it = s.begin(); it != s.end(); ++it){
cout << *it << " ";
}
cout << endl; for (string::reverse_iterator it = s.rbegin(); it != s.rend(); ++it){
cout << *it << " ";
}
cout << endl;
}
int main(){ //test01();
//test02();
//test03();
test04();
//test05();
//test06();
test07();
test08();
test09();
//test10();
//test11();
return 0;
}

最新文章

  1. 如何在windows 10 x64安装佳能 CP900 驱动
  2. 如何用 CSS 做到完全垂直居中
  3. Show or Hide Menu List via ng-show
  4. 【待补】java开发Web Service
  5. Linux基本操作 9----- 认识与学习bash
  6. CentOS 如何修改mysql 用户root的密码
  7. Example007关闭窗口时关闭父窗口
  8. 在ASP.NET Core中构建路由的5种方法
  9. react-navigation使用之嵌套和跳转
  10. js数组基础
  11. 写给踏入IT行业的自己
  12. 转:select2 使用教程(简)
  13. XML文件的解析—DOM、SAX
  14. 用多线程处理FTP上传
  15. (25)线程---local数据隔离
  16. liunx系统部署
  17. CentOS7安装openjdk、tomcat和mysql流程介绍
  18. python 调用 C语言函数
  19. mysql的联表删除
  20. 『TensotFlow』RNN/LSTM古诗生成

热门文章

  1. 使Squashfs可写的办法
  2. 仿B站小火箭发射上升
  3. 知识图谱-生物信息学-医学顶刊论文(Briefings in Bioinformatics-2021):MPG:一种有效的自我监督框架,用于学习药物分子的全局表示以进行药物发现
  4. vue-axios更改操作
  5. 论文笔记 - PRISM: A Rich Class of Parameterized Submodular Information Measures for Guided Subset Selection
  6. jupyter初体验
  7. python 类相关 下划线相关 __init__
  8. git的介绍、git的功能特性、git工作流程、git 过滤文件、git多分支管理、远程仓库、把路飞项目传到远程仓库(非空的)、ssh链接远程仓库,协同开发
  9. zk系列三:zookeeper实战之分布式锁实现
  10. 定制ASP.NET Core的身份认证