#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插入的更多相关文章

  1. Problem I: STL——多重集的插入和删除

    Problem I: STL--多重集的插入和删除 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1729  Solved: 1258[Submit][ ...

  2. Oracle中,如何将String插入到BLOB类型字段

    1,String插入到BLOB类型字段,(这里的字符串以生成的XML为例): String XML = document.asXML();  //使用dom4j写成的xml是String类型,记得st ...

  3. STL之string容器

    string string封装了char*,管理这个字符串,是一个char*型的容器. string的相关操作 头文件 #include<string> string构造函数 string ...

  4. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  5. C++STL之string (转)

    在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1;      默认构造函数,s1为空串 string ...

  6. C++STL之String

    本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...

  7. STL之string使用简介

    声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...

  8. C++ STL介绍——String类

    目录 1.简介 2.string类成员函数汇总 3.String类的构造函数以及析构函数 4.获取字符串长度 5.获取字符串元素 6.字符串比较方法 7.字符串输入输出 8.字符串查找函数 1.简介 ...

  9. STL的string和wstring

    STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...

随机推荐

  1. C++中的string

    要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...

  2. WPF:向客户端发出某一属性值已更改的通知INotifyPropertyChanged接口

    Person.cs using System.ComponentModel; namespace _01_INotifyPropertyChanged { class Person:INotifyPr ...

  3. codevs 1028 花店橱窗布置 KM算法

    题目链接 n个花, m个花瓶, 每个花放到一个花瓶里会产生一个值w[i][j], 一个花只能放到一个花瓶里, 一个花瓶只能放一个花, 求产生的最大值. 带权二分图模板. #include <io ...

  4. MacOS copy图标shell脚本

    不会shell  同学做的... 可以看见在当前文件夹下创建了一个icons文件夹 最后还压缩了文件夹 #!/bin/bash # readPlist [plist] [key] function r ...

  5. JavaScript 反柯里化

    浅析 JavaScript 中的 函数 uncurrying 反柯里化 柯里化 柯里化又称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函数接受剩下的参数,这中间 ...

  6. IIS启动网站

    在启动一个网站前要先打开一个服务.可是Windows 的提示非常的奇怪: “除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法 ...

  7. 【转】android权限列表

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...

  8. 自己模拟实现math.h中的函数

    之前一直都很迷惑pow()函数时怎么实现的,对于整数次的幂我还能很容易做到,但是对于分数次幂就不是那么好做了.需要一些高等数学级数的知识. 我这里实现了求ln(x), pow(double x, do ...

  9. MySQL Binlog的介绍

    binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 作用:MySQL的作用类似于Oracle的归档日志,可以用来查看数据库的变 ...

  10. WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起

    WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的一个JavaScript绑定,WebGL可以为HTM ...