STL之string插入
2015-06-22 21:25
267 查看
#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(8, '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(0, 3, 'A'); cout << s <<endl; s.insert(5, s2); //位置从0开始,第6个元素之前 cout << s <<endl; s2 = "12345"; s.insert(0, s2, 2, 3); cout << s <<endl; char *cp = "Stately plump Buck"; s.assign(cp, 7); cout << s <<endl; s.assign(cp); cout << s <<endl; s = "hello"; s.insert(0, cp, 7); cout << s <<endl; s = "hello"; s.insert(0, cp); cout << s <<endl; s = "hello"; s2 = "abcdef"; s.assign(s2, 2, 3); cout << s << endl; s.erase(2, 3); cout << s <<endl; s = "123456789"; s.erase(s.size()-5, 1); //删除字符串的倒数第5个; cout << s <<endl; s.insert(s.size(), 5, '!'); //size()最后一个的后一个,insert在size()之前插入; cout << s <<endl; s = "abc"; s.erase(0, 1).insert(0, "A"); cout << s <<endl; s = "abc"; s[0] = 'A'; cout << s <<endl; return 0; }
只适合string的操作:
获取子串:substr()函数;--------3个重载函数
替换函数:replace()函数;-------6个重载函数
尾加函数:append()函数;--------10个重载函数;
#include <iostream> #include <string> using namespace std; int main() { string s("hello world"); string s2 = s.substr(6, 5); //从第7个位置开始的5个字符; string s3 = s.substr(6, 50); //从第7个位置开始的50个字符,实际上达不到50个; cout << s3 << endl; s2 = s.substr(6); cout << s2 << endl; s = "C++ Primer"; s.append(" 3rd Ed. "); cout << s << endl; s.insert(s.size(), " 3rd Ed. "); cout << s << endl; s.replace(11, 3, "4th"); //字符串替换 cout << s << endl; s.replace(11, 3, "AAAAAA"); cout << s << endl; return 0; }
#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("0123456789"); string::size_type pos = 0; //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 = 0; 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 0; }
相关文章推荐
- 两个栈实现一个队列
- 端口扫描-调用NC扫描
- 第二次冲刺团队进展报告二
- 读Pyqt4教程,带你入门Pyqt4 _012
- 内存对齐的那点事
- [leetcode] 85.Maximal Rectangle
- 阶段总结
- 练习代码(五)接口与内部类(一)
- Android基础知识_Activity之间传递参数
- 第二次冲刺团队进展报告一
- Java 求字符串中出现频率最高字符
- PPT制造电子相册
- codeforces #306 550B B. Preparing Olympiad(位压缩枚举)
- JVM--总体理解
- 冒泡排序
- oracle 数据类型详解---日期型
- 手机屏幕抖动的形式实现
- C#实现任意大数的计算和简单逻辑命题的证明——前言
- Android中scrollview和viewpage滑动冲突解决
- LTE中的HARQ学习(3)——上行HARQ