STL 小白学习(2) string
#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的更多相关文章
- STL 小白学习(10) map
map的构造函数 map<int, string> mapS; 数据的插入:用insert函数插入pair数据,下面举例说明 mapStudent.insert(pair<, &qu ...
- STL 小白学习(9) 对组
void test01() { //构造方法 pair<, ); cout << p1.first << p1.second << endl; pair< ...
- STL 小白学习(1) 初步认识
#include <iostream> using namespace std; #include <vector> //动态数组 #include <algorithm ...
- STL 小白学习(8) set 二叉树
#include <iostream> using namespace std; #include <set> void printSet(set<int> s) ...
- STL 小白学习(7) list
#include <iostream> using namespace std; #include <list> void printList(list<int>& ...
- STL 小白学习(5) stack栈
#include <iostream> #include <stack> //stack 不遍历 不支持随机访问 必须pop出去 才能进行访问 using namespace ...
- STL 小白学习(6) queue
//queue 一端插入 另一端删除 //不能遍历(不提供迭代器) 不支持随机访问 #include <queue> #include <iostream> using nam ...
- STL 小白学习(4) deque
#include <iostream> #include <deque> //deque容器 双口 using namespace std; void printDeque(d ...
- STL 小白学习(3) vector
#include <iostream> using namespace std; #include <vector> void printVector(vector<in ...
随机推荐
- linux-grep-tail-find
如果在只是想匹配模式的上下几行,grep可以实现. $grep -5 'parttern' inputfile //打印匹配行的前后5行 $grep -C 5 'parttern' inputfile ...
- eclipse 中配置php的 XDebug调试
1. 打开 eclipse for php IDE,window->preference->PHP->Debug 2. 配置phpserver 3. 我的已经增加好了,默认的应该有l ...
- JavaScript 事件之event.preventDefault()与event.stopPropagation()简单介绍
event.preventDefault()用法介绍: 该方法将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作). 例如,如果 type 属性是 “submit”,在事件传播的任 ...
- 从Joda-Time反观Java语言利弊
基本上每个企业应用系统都涉及到时间处理.我们知道,以前用java原生的Date+Calendar非常的不方便.后来Joda-Time诞生,这个专门处理日期/时间的库提供了DateTime类型,用它可以 ...
- Python str byte 互相转换
- sublime设置tab为四个空格
在首选项->设置里: 添加: "tab_size": 4, "translate_tabs_to_spaces": true,
- OpenStack-RabbitMQ-获取vm、磁盘、网络设备的状态变化
需求 及时知道vm,硬盘,镜像,网络设备.负载均衡器状态变化 分析 Dashboard中也是通过定时使用ajax调用API来获取虚拟机的状态信息的定时轮训的方式过于被动 解决方案 共用rabbitmq ...
- IntelliJ Idea 使用笔记
1. IntelliJ Idea解决Could not autowire. No beans of 'xxxx' type found的错误提示. 原因可能有两个,第一个是IntellijIDEA本身 ...
- git忽略某些文件的几种方法
不知道为什么我记得我写过这个内容但是又找不到了,只好照着大致记忆写一下以备留存. 1.首先肯定是.gitignore文件 .gitignore文件适合在文件尚未被追踪时加入,将其忽略便可以不上传到远程 ...
- Guitar Pro里自动化编辑器有什么用?如何使用?
我们在使用操作Guitar Pro来进行吉他学习创作时,会遇到下面几个问题.当我们想要改变全部小节或者某一特定小节的拍速时,就会在想,有没有什么简便工具来实现我们的想法呢?告诉大家,Guitar Pr ...