#include <iostream>
using namespace std;
#include <string> //初始化操作
void test01() {
//初始化操作
string s1;
string s2(, 'a');
string s3("abc");
string s4(s3); cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl; }
//赋值操作
void test02() {
//赋值操作
string s1;
string s2("appasd");
s1 = "";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1 = 'a';
cout << s1 << endl; //成员方法 assign
s1.assign("jsk");
cout << s1 << endl; } //取值操作
void test03() {
//取值操作
string s1 = "abashjdsa"; //重载[]操作符
for (int i = ; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl; //也提供了成员方法
for (int i = ; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}
cout << endl; //区别
//[] 访问越界直接挂
//at(i) 抛出异常
try {
cout << s1.at()<<endl;
}
catch (...) {
cout << "越界 Error...!" << endl;
} }
//拼接操作
void test04() {
//+= 重载
string s = "";
cout << s << endl;
s += "";
cout << s << endl;
string s2 = "abc";
s += s2;
cout << s << endl;
//append 成员函数重载
string s3 = "";
s.append(s3);
cout << s << endl;
//等号重载
string s4 = s + s3;
cout << s4 << endl; } //string 查找与替换
void test05() {
string s = "fgabcdefg";
//s.find 查找第一次出现的位置
int pos = s.find("z");
cout << "pos: " << pos << endl;//没有 return -1
//s.rfind 查找最后一次出现的位置
pos = s.rfind('g');
cout << "pos: " << pos << endl; }
//替换
void test06() {
//替换
string s = "abcdefg";
s.replace(, , ""); //将0位置开始的2个 ab替换成1111
cout << s << endl;
}
//比较
void test07() {
//比较 相等返回0
string s1 = "abcd";
string s2 = "abc4"; if (s1.compare(s2) == ) {
cout << "相等" << endl;
}
else {
cout << "不相等!" << endl;
} }
//字串操作
void test08() {
string s = "abcsdsfsadas";
string substring = s.substr(, );//取s的从1位置开始的3个字符的字串
cout << substring << endl; }
//插入 删除
void test09() {
string s = "asfasddfsa";
s.insert(, "");//在s[1]位置前插入123
cout << s << endl;
s = "";
s.erase(, );//删除 s[0]开始的4个字符
cout << s << endl;
}
int main() {
test09();
}

STL 小白学习(2) string的更多相关文章

  1. STL 小白学习(10) map

    map的构造函数 map<int, string> mapS; 数据的插入:用insert函数插入pair数据,下面举例说明 mapStudent.insert(pair<, &qu ...

  2. STL 小白学习(9) 对组

    void test01() { //构造方法 pair<, ); cout << p1.first << p1.second << endl; pair< ...

  3. STL 小白学习(1) 初步认识

    #include <iostream> using namespace std; #include <vector> //动态数组 #include <algorithm ...

  4. STL 小白学习(8) set 二叉树

    #include <iostream> using namespace std; #include <set> void printSet(set<int> s) ...

  5. STL 小白学习(7) list

    #include <iostream> using namespace std; #include <list> void printList(list<int>& ...

  6. STL 小白学习(5) stack栈

    #include <iostream> #include <stack> //stack 不遍历 不支持随机访问 必须pop出去 才能进行访问 using namespace ...

  7. STL 小白学习(6) queue

    //queue 一端插入 另一端删除 //不能遍历(不提供迭代器) 不支持随机访问 #include <queue> #include <iostream> using nam ...

  8. STL 小白学习(4) deque

    #include <iostream> #include <deque> //deque容器 双口 using namespace std; void printDeque(d ...

  9. STL 小白学习(3) vector

    #include <iostream> using namespace std; #include <vector> void printVector(vector<in ...

随机推荐

  1. java框架之MyBatis(2)-进阶&整合Spring&逆向工程

    进阶内容 准备 jdbc.url=jdbc:mysql://192.168.208.192:3306/test?characterEncoding=utf-8 jdbc.driver=com.mysq ...

  2. PHP 二位数组按照下标排序

    1.排序得内容 array(6) { [0] => array(12) { [0] => string(3) "160" [1] => string(2) &qu ...

  3. java 和 c++ 实现的各种基础数据结构和算法

    https://github.com/phishman3579/java-algorithms-implementation https://github.com/xorz57/forest

  4. VUE—打印(原生态网页打印)

    //打印触发的方法 print(e){ let subOutputRankPrin = document.getElementById('printcode'); var options = { fo ...

  5. Tomcat服务安全加固

    Tomcat服务默认启用了管理后台功能,使用该后台可直接上传 war 文件包对站点进行部署和管理.由于运维人员的疏忽,可能导致管理后台存在空口令或者弱口令的漏洞,使得黑客或者不法分子可以利用该漏洞直接 ...

  6. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. vue-amap | 基于 Vue 2.x 与高德的地图组件

    vue-amap | 基于 Vue 2.x 与高德的地图组件 参考:https://elemefe.github.io/vue-amap/#/

  8. HDU 2586 How far away(LCA+邻接表)

    How far away &题解: 和上篇是一样的题,这用的是lca方法做的, 不知道为什么,把数组开到80000 就a了 >_< 哈 我现在知道为什么了,因为我的rmq数组没有乘 ...

  9. linux挂载概念简述:

    挂载概念简述: 根文件系统之外的其他文件要想能够被访问,都必须通过“关联”至根文件系统上的某个目录来实现,此关联操作即为“挂载”,此目录即为“挂载点”,解除此关联关系的过程称之为“卸载” 1.挂载:根 ...

  10. SVN拉分支,合并分支

    前提是:本地已安装SVN,且在SVN中新建好branch和tag目录 拉分支: 把svn内容下载到本地,然后右键TortoiseSVN-->Branch/tag... 上图中红框选择存放的路径, ...