STL string 常见用法详解
string 常见用法详解
1. string 的定义
//定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可
string str;
//如果要初始化,可以直接给string类型的变量进行赋值
string str = "abcd";
2. string 中内容的访问
**(1) 通过下标访问 **
//一般来说,可以直接像字符数组那样去访问string
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string str = "abcd";
for(int i = 0; i < str.length(); i++) {
printf("%c ", str[i]); //输出abcd
}
return 0;
}
//如果要读入和输出整个字符串,则只能用 cin 和 cout
#include <iostream>
#include <string>
using namespace std;
int main() {
strng str;
cin >> str;
cout << str;
return 0;
}
//使用printf来输出string
//即用c_str()将stringelixir转换为字符数组进行输出
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string str = "abcd";
printf("%s\n", str.c_str()); //将string型str使用c_str()变为字符数组
return 0;
}
**(2) 通过迭代器访问 **
//定义迭代器
string::iterator it;
//通过*it来访问string里的每一位
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string str = "abcd";
for(string::iterator it = str.begin(); it != str.end(); it++) {
printf("%c", *it);
}
return 0;
}
//string和vector一样,支持直接对迭代器进行加减某个数字
3. string 常用函数实例解析
**(1) operator+= **
//这是string的加法,可以将两个string直接拼接起来
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "abc", str2 = "xyz", str3;
str3 = str1 + str2; //将str1和str2拼接,赋值给str3
str1 += str2; //将str2直接拼接到str1上
cout << str1 << endl;
cout << str3 << endl;
return 0;
}
**(2) compare operator **
//两个string类型可以直接使用==, !=, <, <=, >, >=比较大小
//比较规则是字典序
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string str1 = "aa", str2 = "aaa", str3 = "abc", str4 = "xyz";
if(str1 < str2) printf("ok1\n"); //如果str1字典序小于str2,输出ok1
if(str1 != str3) printf("ok2\n"); //如果str1字典序不等于str3,输出ok2
if(str4 >= str3) printf("ok3\n"); //如果str4字典序大于等于str3,输出ok3
return 0;
}
(3) length()/size()
//length()返回string长度,即存放的字符数,时间复杂度为O(1)
//size() 与 length()基本相同
string str = "abcxyz";
printf("%d %d\n", str.length(), str.size());
(4) insert()
//string的insert()函数有很多种写法,这里给出几个常用的写法时间复杂度为O(N)
// 1. insert(pos, string), 在pos号位置插入字符串string
string str = "abcxyz", str2 = "opq";
str.insert(3, str2); //往str[3]处插入opq,这里str2的位置直接写"opq"也是可以的
cont << str << endl;
// 2. insert(it, it2, it3)
//it 为原字符串的欲插入位置, it2 和 it3 为待插字符串的首尾迭代器,用来表示串[it2, it3)将被插在it的位置上
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abcxyz", str2 = "opq"; //str是原字符串,str2是待插字符串
//string的3号位(即c和x之间)插入str2
str.insert(str.begin() + 3, str2.begin(), str2.end());
cout << str << endl;
return 0;
}
**(5) erase() **
//erase()有两种用法:删除单个元素,删除一个区间的所有元素。时间复杂度均为O(N)
// 1. 删除单个元素
//str.erase(it)用于删除单个元素,it为需要删除的元素的迭代器
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abcdefg";
str.erase(str.begin() + 4); //删除4号位(即 e)
cout << str << endl;
return 0;
}
// 2. 删除一个区间内的所有元素
//删除一个区间内的所有元素有两种方法
//str.erase(first, last)
//其中first为需要删除的区间的其实迭代器,而last则为需要删除的的区间的末尾迭代器的下一个地址
//即为删除[first, last)
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abcdefg";
//删除[str.begin() + 2, str.end() - 1)】内的元素,即cedf
str.erase(str.begin() + 2, str.end() - 1);
cout << str << endl;
return 0;
}
//str.erase(pos, length)
//其中pos为需要开始删除的起始位置,length为删除的字符个数
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abcdefg";
str.erase(3, 2); //删除从3号位开始的2个字符,即de
cout << str << endl;
return 0;
}
**(6) clear() **
//clear()用以清空string中的数据,时间复杂度一般为O(1)
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string str = "abcd";
str.clear(); //清空字符串
printf("%d\n", str.length());
return 0;
}
**(7) substr() **
//substr(pos, len)返回从pos号位开始,长度为len的字串,时间复杂度为O(N)
#include <iostream>
#include <string>
using namespace std
int main() {
string str = "Thank you for your smile.";
cout << str.substr(0, 5) << endl;
cout << str.substr(14, 4) << endl;
cout << str.substr(19, 5) << endl;
}
(8) string::npos
//string::npos是一个常数,其本身的值为-1
//但由于是unsigned_int类型,因此实际上也可以认为是unsigned_int类型的最大值
//string::npos用以作为find函数失配时的返回值。
//下面实例中可以认为string::pos等于-1或者4295967295
#include <iostream>
#include <string>
using namespace std;
int main() {
if(string::npos == -1) {
cout << "-1 is true." << endl;
}
if(string::npos == 4294967295) {
cout << "4294967295 is also true." << endl;
}
return 0;
}
(9) find()
//str::find(str2), 当str2是str的子串时,返回其在str中第一次出现的位置
//如果str2不是str的子串,那么返回string::npos
//str::find(str2, pos), 从str的pos号位开始匹配str2, 返回值与上相同
//时间复杂度为O(nm),其中n和m分别为str和str2的长度
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Thank you for your smile.";
string str2 = "you";
string str3 ="me";
if(str.find(str2) != string::npos) {
cout << str.find(str2) << endl;
}
if(str.find(str2, 7) != string::npos) {
cout << str.find(str2, 7) << endl;
}
if(str.find(str3) != string::npos) {
cout << str.find(str) << endl;
} else {
cout << "I know there is no position for me." << endl;
}
return 0;
}
(10) replace()
//str.replace(pos, len, str2)把str从pos号位开始,长度为len的子串替换为str2
//str.replace(it1, it2, str2)把str的迭代器[it1, it2)范围的子串替换为str2
//时间复杂度为O(str.length())
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Maybe you will turn around.";
string str2 = "will not";
string str3 = "surely";
cout << str.replace(10, 4, str2) << endl;
cout << str.replace(str.begin(), str.begin() + 5, str3) << endl;
return 0;
}
STL string 常见用法详解的更多相关文章
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- STL map 常见用法详解
<算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...
- STL set 常见用法详解
<算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...
- STL pair 常见用法详解
<算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...
- STL stack 常见用法详解
<算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...
- STL queue 常见用法详解
<算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
- C++标准模板库(STL)——queue常见用法详解
queue的定义 queue<typename> name; queue容器内元素的访问 由于队列本身就是一种先进先出的限制性数据结构,因此在STL中只能通过front()来访问队首元素, ...
- C++标准模板库(STL)——map常见用法详解
map的定义 map<typename1, typename2> mp; map需要确定映射前类型和映射后类型,所以需要在<>内填写两个类型,第一个是键的类型,第二个是值的类型 ...
随机推荐
- Codeforces 1238E. Keyboard Purchase
传送门 注意到 $m$ 只有 $20$ ,考虑一下状压 $dp$ 设 $f[S]$ 表示当前确定的字符集合为 $S$ ,那么转移就考虑从最右边加入的下一个字符 $c$ 那么问题来了,代价如何计算 考虑 ...
- 预处理、const、static与sizeof-为什么不把所有的函数都定义成内联函数
1:内联是以代码膨胀(复制)为代价的,仅仅省去了函数调用的开销,从而提高函数的执行效率.如果执行函数体内代码的时间相比于函数调用的开销较大,那么效率的收获会很小.另一方面,每一处内联函数的调用都要复制 ...
- Consider defining a bean of type `xxx` in your configuration问题解决
在使用SpringBoot装配mybatis时出现了异常 *************************** APPLICATION FAILED TO START *************** ...
- Linux设备驱动程序 之 read和write
read和write原型 read和write方法完成的任务是相似的,亦即,拷贝数据到应用程序空间,或者反过来从应用程序空间拷贝数据:因此,它们的原型很相似,如下: ssize_t (*read) ( ...
- git上传超过100m大文件
1.git出错如下错误时 执行如下可解决错误: git rm --cache '大文件路径' git commit --amend -CHEAD git push 2.当必须上传大文件时.需借助git ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
- decimal模块 --数字的精度、保留小数位数、取整问题
开始之前需要注意一点是:精度值为数字的总位数,如:1.23, 精度值为3: 0.123,精度值也为3 1.更改默认精度值后,直接进行计算即可保留对应精度值 from decimal import ge ...
- VS2015编译cef3-2357
1.会遇到把警告当错误的情况 按照如下设置即可 2. 错误 C2334 “:”的前面有意外标记:跳过明显的函数体 (编译源文件 E:\cef_binary_3.2357.1271.g8e0674e_w ...
- Sublime text3中文版 无法安装插件There are no packages available for installation问题的解决。
说起来差点没被气死,我当时的情况已经是要疯了,连他们的域名都ping不通,我还想着,咋地,要倒闭了? 首选项->插件设置->Package Control->默认 里边的这个配置项 ...
- mysql查询json字段
一张test表里存了一个content字段是json类型的,查询该content里manualNo这个字段 select JSON_EXTRACT (test .content, '$.manualN ...