STL之string插入
#include <iostream>
#include <string> using namespace std;
int main()
{
string s("hello");
string s2("abcdef"); string::iterator p = s.begin();
cout << *p << endl;
s.insert(p, 'A'); //插入之后,p指向新插入的数据
cout << *p << endl;
cout << s << endl; //每执行插入操作一次,gcc下必须重新给迭代器赋值
//否则内存泄漏
// 为什么呢?不明白
p = s.begin();
s.insert(p,'B');
cout << *p << endl;
cout << s << endl; string::iterator b = s2.begin();
string::iterator e = s2.end(); p = s.begin();
s.insert(p, b, e);
cout << s << endl; s = "hello";
cout << s <<endl;
s.assign(b, e);
cout << s <<endl; s.assign(, 'k');
cout << s <<endl; s = "abcdef";
p = s.begin();
s.erase(p); //删除
cout << s <<endl; p = s.begin();
p++;
p++;
string::iterator p2 = s.end();
p2--;
s.erase(p, p2);
cout << s <<endl; s = "hello";
s2 = "abc";
s.insert(, , 'A');
cout << s <<endl; s.insert(, s2); //位置从0开始,第6个元素之前
cout << s <<endl; s2 = "";
s.insert(, s2, , );
cout << s <<endl; char *cp = "Stately plump Buck";
s.assign(cp, );
cout << s <<endl;
s.assign(cp);
cout << s <<endl; s = "hello";
s.insert(, cp, );
cout << s <<endl;
s = "hello";
s.insert(, cp);
cout << s <<endl; s = "hello";
s2 = "abcdef";
s.assign(s2, , );
cout << s << endl; s.erase(, );
cout << s <<endl; s = "";
s.erase(s.size()-, ); //删除字符串的倒数第5个;
cout << s <<endl; s.insert(s.size(), , '!'); //size()最后一个的后一个,insert在size()之前插入;
cout << s <<endl; s = "abc";
s.erase(, ).insert(, "A");
cout << s <<endl; s = "abc";
s[] = 'A';
cout << s <<endl;
return ;
}
只适合string的操作:
获取子串:substr()函数;--------3个重载函数
替换函数:replace()函数;-------6个重载函数
尾加函数:append()函数;--------10个重载函数;
#include <iostream>
#include <string> using namespace std; int main()
{
string s("hello world");
string s2 = s.substr(, ); //从第7个位置开始的5个字符;
string s3 = s.substr(, ); //从第7个位置开始的50个字符,实际上达不到50个;
cout << s3 << endl; s2 = s.substr();
cout << s2 << endl; s = "C++ Primer";
s.append(" 3rd Ed. ");
cout << s << endl; s.insert(s.size(), " 3rd Ed. ");
cout << s << endl; s.replace(, , "4th"); //字符串替换
cout << s << endl; s.replace(, , "AAAAAA");
cout << s << endl; return ;
}
#include <iostream>
#include <string> using namespace std; int main()
{
string name("AnnmaBelle");
string::size_type pos1= name.find("Bell");
if(pos1 == string::npos){
cout << "not find" << endl;
}else{
cout << "find it, pos: " << pos1 << endl; //pos1 = 4;
} name = "r2d3";
string num("");
string::size_type pos = ;
//find_first_of(), 查找name中的任意一个字符即返回;
//查找name中数字,反之可以找字母
//与之对应的是:find_last_of
while((pos = name.find_first_of(num, pos))!=string::npos){
cout << name[pos] << endl;
pos++;
} //find_first_not_of(),从num中查找name中字符,没有找到即返回string::npos
//与之对应的是:find_last_not_of
pos = ;
while((pos=name.find_first_not_of(num, pos))!=string::npos){
cout << name[pos] << endl;
++pos;
} string river("Mississippi");
string::size_type first_pos = river.find("is"); //从前向后操作,找到第一个即返回,返回值是下标
if(first_pos != string::npos)
cout <<"first_pos: "<< first_pos << endl;
first_pos = river.rfind("is"); //从后向前操作,找到第一个即返回,返回值是下标
if(first_pos != string::npos)
cout <<"last_pos: "<< first_pos << endl; return ;
}
STL之string插入的更多相关文章
- Problem I: STL——多重集的插入和删除
Problem I: STL--多重集的插入和删除 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1729 Solved: 1258[Submit][ ...
- Oracle中,如何将String插入到BLOB类型字段
1,String插入到BLOB类型字段,(这里的字符串以生成的XML为例): String XML = document.asXML(); //使用dom4j写成的xml是String类型,记得st ...
- STL之string容器
string string封装了char*,管理这个字符串,是一个char*型的容器. string的相关操作 头文件 #include<string> string构造函数 string ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- C++STL之string (转)
在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串 string ...
- C++STL之String
本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...
- STL之string使用简介
声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...
- C++ STL介绍——String类
目录 1.简介 2.string类成员函数汇总 3.String类的构造函数以及析构函数 4.获取字符串长度 5.获取字符串元素 6.字符串比较方法 7.字符串输入输出 8.字符串查找函数 1.简介 ...
- STL的string和wstring
STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...
随机推荐
- 亲测的一款在线作图神器:ProcessOn
本人近日发现一款作图神器:ProcessOn 它是一款在线的作图工具,完全国产,前台是用HTML5 Canvas加javascript做绘图,后台用java实现数据处理和图片生成, 整站UI基本类似 ...
- html 转 js 字符串
看到一个牛人的博客 http://riny.net/lab/#tools_html2js 看了下他的代码 挺棒的 所依赖的两个库在这里 https://github.com/Bubblings/l ...
- 使用PyQt来编写第一个Python GUI程序
原文:使用PyQt来编写第一个Python GUI程序 本文由 伯乐在线 - Lane 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:pythonforengineers.com.欢迎加 ...
- Linux常用的系统监控shell脚本
http://www.linuxqd.com下面是我常用的几个Linux系统监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助.1.查看主机网卡流量 #!/bin/bash #netw ...
- ecshop删除商品函数
/** * 从回收站删除多个商品 * @param mix $goods_id 商品id列表:可以逗号格开,也可以是数组 * @return void */ function delete_goods ...
- HDU 2588 GCD
题目大意:给定N,M, 求1<=X<=N 且gcd(X,N)>=M的个数. 题解:首先,我们求出数字N的约数,保存在约数表中,然后,对于大于等于M的约数p[i],求出Euler(n/ ...
- hdu 2563 统计问题
统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- ural 1057(数位dp)
数位dp题,关键是用树的思维去考虑. 对于一个数字X,要是能表示成K个B的不同次幂,等价于X在B进制下有且只有K个位上面的数字为一,其他位上的数字都为0. 具体读者可以去参考,国家集训队李聪的论文,里 ...
- Libcurl安装及编译
1.安装curl wget http://curl.haxx.se/download/curl-7.26.0.tar.gz tar -zxvf curl-7.26.0.tar.gz cd curl- ...
- 浮点数(double)的优势
用 double 处理大数据 , 即使字符串位数超百位,同样可以用 sscanf 或 atof 来处理,却不会变成负数,原因是浮点数可以用指数表示,但精度会丢失,下面介绍利用这个性质解决的一些问题: ...