#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++学习之友元类和友元函数

    C++学习之友元类和友元函数       模板类声明也可以有友元,模板的友元可以分为以下几类:        1.非模板友元:        2.约束模板友元,即就是友元的类型取决于类被实例化的时候的 ...

  2. Nanjing GDG Meetup 10月19日线下活动

     Nanjing GDG 10 月份的线下活动将在本周六 (10/19) 举办,这次会请到对所有开发者都有重要帮助的神器 GoAgent 的作者 Phus Lu 来给我们做一场分享,热烈欢迎大家报名参 ...

  3. js中递归解析xml

    xml结构: <RightMenuItems>  <Item Code="New" Name="新建" GroupCode="Edi ...

  4. JavaScript 入門

    <html lang="en"> <head>   <meta charset="UTF-8">   <meta na ...

  5. Java NIO read/write file through FileChannel

    referee:  Java NIO FileChannel A java nio FileChannel is an channel that is connected to a file. Usi ...

  6. Windows 7各版本的主要功能区别是什么 有何不同

    Windows 7包含6个版本,分别为Windows 7 Starter(初级版).Windows 7 Home Basic(家庭普通版).Windows 7 Home Premium(家庭高级版). ...

  7. Windows 8.1 正式版微软官方原版镜像下载(新增10/17新版下载)

    中文版:中国区OEM预装版本,特定国家版,锁定语言,其它功能和核心版没有区别.简体中文单语言版:锁定语言,其它功能和核心版没有区别.专业版+核心版[零售版][推荐]:镜像内包含专业版(Professi ...

  8. HelloX项目github协同开发指南

    概述 为了提高协同开发效率,HelloX项目已托管到github网站上.根据目前的开发进展,创建了下列几个子项目: HelloX操作系统内核项目:https://github.com/hellox-p ...

  9. 注册UBER(优步)司机常见问题,如何注册uber(优步)司机

    如何注册uber(优步)司机,怎么注册UBER(优步)司机 此链接为优步内部人员推荐,优先审核,基本当天就能收到短信,注意上传证件照清晰点. https://uber.avosapps.com/ref ...

  10. Linux新手笔记 sudo

    centos 6.4 32bit 你是也像我一样,厌烦了在root用户和个人用户之间来回切换.或者干脆直接用root用户.可以这样设置,然后在命令前加sudo 即可使用自己到密码,临时用root身份执 ...