#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. JDBC-DAO层数据访问工具类的实现

    private static PreparedStatement pst; private static ResultSet rst; public static <T> int inse ...

  2. WinSDK(菜单笔记)

  3. 2014年西安区域赛的几道水题(A. F. K)

    A . 问一组数能否全部被3整除 K. S1 = A, S2 = B, Si = |Si-1  -  Si-2|; 一直循环问, 出现了多少不同的数: 多模拟几组数, 可以发现和辗转相除法有很大关系 ...

  4. JavaWeb-----实现第一个Servlet程序

    1.Servlet简介      Servlet是在服务器端运行的一个小程序,实际上一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问的这个驻留在服务器内 存里的servl ...

  5. winfrom进程、线程、用户控件

    一.进程 一个进程就是一个程序,利用进程可以在一个程序中打开另一个程序. 1.开启某个进程Process.Start("文件缩写名"); 注意:Process要解析命名空间. 2. ...

  6. mybatis的sql映射文件不能打包进目录解决办法

    方法二: <build> <finalName>qwe</finalName> <plugins> <plugin> <groupId ...

  7. IP白名单的实现(PHP)

    有些项目可能会用到一个IP地址的白名单黑名单之类的验证. 比如,只有IP地址在白名单中,才可以访问该系统. 那么此时,白名单的维护,一般是一个文件,里边是一些IP地址(每行一个IP),当然也有的可能是 ...

  8. 运行软件出现:模块“msvcp110.dll”已加载,但找不到入口点DllRegister

    根据百度大多数回答来说 1:先是出现   无法启动程序,因为计算机丢失mfc110.dll 尝试重新安装该程序以解决问题 错误处理:下载或者在别人电脑上拷一份 如:'msvcp110.dll‘ 这类文 ...

  9. nginx和tomcat的优化

    测试脚本(服务器414报错)#!/bin/bashurl=http://192.168.4.5/for i in {1..5000}do url=${url}v$i=idoneecho $url #a ...

  10. LOJ #10132. 「一本通 4.4 例 3」异象石

    题目地址 LOJ 题解 神仙思路.思路参考自<算法竞赛进阶指南>. 考虑维护dfs序中相邻两个石头的距离,那么每次?的答案就是sum/2(首尾算相邻) 然后维护一下拿个平衡树/set维护一 ...