#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;
}

STL string常用API的更多相关文章

  1. String常用API

    String常用API 1. 获取字符串长度 int length = str.length(); 2. 根据索引,返回字符串中对应的字符 char c = str.chaeAt(length-1); ...

  2. STL vector常用API

    1.容器:序列容器(时间决定).关联式容器(容器中的数据有一定规则) 2.迭代器:通过迭代器寻找.遍历容器中的数据 vetor的使用:数据遍历与输出 #define _CRT_SECURE_NO_WA ...

  3. stl string常用函数

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  4. STL string 常用函数(转)

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  5. STL string常用操作指令

    s.insert(pos,args); 在pos之前插入args指定的字符.pos可以是一个下标或一个迭代器.接受下标的版本返回一个指向s的引用;接受迭代器的版本返回指向第一个插入字符的迭代器. s. ...

  6. lua中string常用api

    local a="abcdefgbbb" string.sub(a,1,3) 字符串截取 返回截取的字符串           print(string.sub(a,1,3))   ...

  7. kubernetes 客户端KubeClient使用及常用api

    KubeClient是kubernetes 的C#语言客户端简单易用,KubeClient是.NET Core(目标netstandard1.4)的可扩展Kubernetes API客户端, gith ...

  8. String 字符串详解 / 常用API

    String 详解 / 常用API 简介 String 是不可改变的字符串序列.String 为字符串常量 StringBuilder 与StringBuffer 均为可改变的字符串序列.为字符串变量 ...

  9. 常用API(Object、String、StringBuffer、用户登陆注册)

    常用API 今日内容介绍 u Object u String u StringBuilder 第1章 Java的API及Object类 在以前的学习过程中,我们都在学习对象基本特征.对象的使用以及对象 ...

随机推荐

  1. img通过修改css等比例缩小图片

    css中加上:object-fit:cover 例子: img{ width: 200px; height: 400px; object-fit: cover; }

  2. 对循环神经网络参数的理解|LSTM RNN Input_size Batch Sequence

    在很多博客和知乎中我看到了许多对于pytorch框架中RNN接口的一些解析,但都较为浅显甚至出现一些不准确的理解,在这里我想阐述下我对于pytorch中RNN接口的参数的理解. 我们经常看到的RNN网 ...

  3. 在Tomcat中启用虚拟线程特性

    前提 趁着国庆前后阅读了虚拟线程相关的源码,写了一篇<虚拟线程 - VirtualThread源码透视>,里面介绍了虚拟线程的实现原理和使用示例.需要准备做一下前期准备: 安装OpenJD ...

  4. 2022网刃杯ics

    ​ 目录 easyiec Ncsubj 喜欢移动的黑客 xyp07 ICS6-LED_BOOM 根据大佬的wp后,自己做了一遍 这次学到很多东西 ICS easyiec tcp追踪流直接能看到 ​编辑 ...

  5. JUC(3)

    文章目录 1.集合类不安全 2.在高并发情况下arraylist()并不安全 3.高并发下set并不安全 3.测试map(高并发情况下出现问题) 1.集合类不安全 2.在高并发情况下arraylist ...

  6. C++ Undefined Behavior 详细列表

    Undefined Behavior,即未定义的行为,指程序不可预测的执行效果,一般由错误的代码实现引起.出于效率.兼容性等多方面原因,语言标准不便于定义错误程序的明确行为,而是将其统称为" ...

  7. Linux的挖矿木马病毒清除(kswapd0进程)

    1.top查看资源使用情况 看到这些进程一直在变化,但是,主要是由于kswapd0进程在作怪,占据了99%以上的CUP,查找资料后,发现它就是挖矿进程 2.排查kswapd0进程 执行命令netsta ...

  8. $_SERVER["REQUEST_URI"],在 PHP 众多预定义服务器变量中,$_SERVER["REQUEST_URI"] 算是经常用到的,但是这个变量只有 apache 才支持

    例如访问:http://localhost/index.php?app=lunbo获取到的$_SERVER["REQUEST_URI"]为"/index.php?app= ...

  9. 一、SQL介绍

    Mysql 简单来说,数据库就是一个存储数据的仓库,它将数据按照特定的规律存储在磁盘上.为了方便用户组织和管理数据,其专门提供了数据库管理系统.通过数据库管理系统,用户可以有效的组织和管理存储在数据库 ...

  10. 【题解】P7860 [COCI2015-2016#2] ARTUR

    题面传送门 好题. 主要思路和另一位巨佬差不多,详细讲一下判断的部分. 解决思路: 首先考虑本题与拓扑排序有和关系.可以想到,某些棍子的先后移动顺序是有限制的.比如: 这里红色的必须比蓝色的先移动,因 ...