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 常见用法详解的更多相关文章

  1. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  2. STL map 常见用法详解

    <算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...

  3. STL set 常见用法详解

    <算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...

  4. STL pair 常见用法详解

    <算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...

  5. STL stack 常见用法详解

    <算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...

  6. STL queue 常见用法详解

    <算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...

  7. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

  8. C++标准模板库(STL)——queue常见用法详解

    queue的定义 queue<typename> name; queue容器内元素的访问 由于队列本身就是一种先进先出的限制性数据结构,因此在STL中只能通过front()来访问队首元素, ...

  9. C++标准模板库(STL)——map常见用法详解

    map的定义 map<typename1, typename2> mp; map需要确定映射前类型和映射后类型,所以需要在<>内填写两个类型,第一个是键的类型,第二个是值的类型 ...

随机推荐

  1. POJ - 3376 Finding Palindromes manacher+字典树

    题意 给n个字符串,两两拼接,问拼接后的\(n\times n\)个字符串中有多少个回文串. 分析 将所有正串插入字典树中,马拉车跑出所有串哪些前缀和后缀为回文串,记录位置,用反串去字典树中查询,两字 ...

  2. Cannot find ./catalina.sh The file is absent or does not have execute permission This file is nee Linux上tomcat无法正常启动

    上传了个tomcat7的压缩包上linux服务器,解压后,想直接启动,发现报错: Cannot find ./catalina.sh The file is absent or does not ha ...

  3. ARTS打卡计划第十四周

    Algorithms: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ Review: “How to write ...

  4. OUC_Summer Training_ DIV2_#7 718

    是18号做的题啦,现在才把报告补上是以前不重视报告的原因吧,不过现在真的很喜欢写报告,也希望能写一些有意义的东西出来. A - Dragons Time Limit:2000MS     Memory ...

  5. k8s应用01-----入门实例

    安装一个单机版的K8S 1.关闭防火墙firewalld2.安装etcd和kubernetesyum install -y etcd kubernetes(会自动安装docker)3.修改配置文件修改 ...

  6. 在react项目中启用mobx的配置方法

    1.安装插件 npm install --save-dev babel-preset-mobx mobx mobx-react 2.package.json配置 "dependencies& ...

  7. DeviceUtils

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> import androi ...

  8. consul ocelot

    consul配置完成后 新建.netcoreapi项目, nuget安装ocelot 添加多个配置文件,.netcore中会自动合并为一个文件,global配置总的配置,其他为各个项目的配置 Serv ...

  9. 003-tomcat配置文件-server、tomcat-users

    1.server.xml讲解 位于conf下 <?xml version="1.0" encoding="UTF-8"?> <!-- Serv ...

  10. Python-sympy科学计算与数据处理(求极限及其它功能)

    极限 其它功能