#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. Notes常用事件整理

    ①      ボタンのクリック事件: Sub Click(Source As Button) Dim ws As New NotesUIWorkspace Dim uidoc As NotesUIDo ...

  2. ThinkPHP第二十二天(表单令牌、相对路径、扩展配置载入、$Think获取系统变量、$_SERVER('HTTP_REFERER')前页地址)

    1.表单令牌开启配置 'TOKEN_ON'=>true 2.相对路径:在thinkphp中,存在单入口index.php,所以程序中的根目录都是以index.php所在的文件夹为根目录,故用./ ...

  3. 星际SC地图制作中生成随机位置,也包括所有需要随机的效果

    星际SC地图制作中生成随机位置,也包括所有需要随机的效果 利用单位 kakaru T 开头那个, kakaru是随机变化位置 注意kakaru的放置位置和占用格子大小,kakaru周围放上LOCATI ...

  4. pyVmomi入门

    简要说明 pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and v ...

  5. JS 节流阀

    JS 节流阀 参考 https://github.com/hahnzhu/read-code-per-day/issues/5 节流阀 节流阀的基本原理 事件函数的执行都记下当前时间, 只有当前时间与 ...

  6. finally块的问题(finally block does not complete normally) (转)

    当finall块中包含return语句时,Eclipse会给出警告“finally block does not complete normally”,原因分析如下: 1.不管try块.catch块中 ...

  7. Android Studio 代码混淆

    新建一个项目,Android Studio默认关闭代码混淆开关,在build.gradle文件中,如下图所示的minifyEnabled 开关,因此如果需要混淆代码,需将false改为true,然后在 ...

  8. SQL Server数据转换【包括Geometry类型】的技巧总结

    1. 字段的组合: update new_master_location set tmp_street_unique=street+'_'+city+'_'+state+'_'+zip+'_'+con ...

  9. [cocos2dx笔记015]关于cocos2dx Button三种状态说明

    经过几天的填坑,最终将现有的项目由cocos2dx 2.2.2移到cocos2dx 3.2,差点放弃3.2了,但在最后一刻,又把坑填平了. cocos2dx 2.x到3.x是一个巨大的变化,能够算是全 ...

  10. Nice way for strip_tags a like

    I found this code works great as the function strip_tags in php to replace html tags from string and ...