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 ...
随机推荐
- 小程序 showModal content换行
wx.showModal({ title: '提示', content: '1.该拼团仅支持到指定取货地址自提\r\n2.拼团支付价格为拼团原价,当到达指定阶梯,拼团差价将在3个工作日内退回您的微信账 ...
- Intellij Idea debug 模式如果发现异常,即添加异常断点在发生异常处
以前用eclipse的时候,可以根据所抛出的异常进行调试,比如:出现了空指针异常,我想知道是哪一行抛出的,在eclipse中我只需在debug模式下把空指针异常这个名字设置进去,当遇到空指针异常时,e ...
- AdPlus
adplus是windbg下面附带的一个小工具: https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/adplus A ...
- Cocos Creator - 入门教程项目 - 博客频道 - CSDN.NET
3457 教程司令部 [20160418] | Cocos Creator - CocoaChina CocoaChina_让移动开发更简单cocoachina.com 2033 Cocos Crea ...
- 关于npm Vue
参考:http://www.runoob.com/w3cnote/vue2-start-coding.html 安装vue脚手架 npm install vue-cli -g 查看当前脚手架版本 np ...
- ServletContext详解(转)
ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放.request,一个用户可有多个:session,一个用户一个:而servletContext,所 ...
- Kali密码攻击之——离线攻击工具
在线密码攻击在渗透测试中很重要,但对于测试过程中得到的哈希,加密数据,又需要离线破解工具辅助解决 Creddump套件 kali下离线攻击工具中的Cache-dump,lsadump,pwdump,均 ...
- qemu 对虚机的地址空间管理
转载:http://huchh.com/2015/06/22/qemu-%E5%AF%B9%E8%99%9A%E6%9C%BA%E7%9A%84%E7%BA%BF%E6%80%A7%E5%9C%B0% ...
- Porsche Piwis Tester II V12.100 Version Released
Piwis Tester II v12.100 Version released today! In this new version we can find the latest type Pors ...
- 安装Joomla!3
在日常测试中搭建一个web 站点进行进行linux 相关功能测试,只是不喜欢httpd 或者nginx 的默认界面: 安装Joomla!3: 系统:centos 7 软件: 连接: https:// ...