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就 ...
随机推荐
- perl正则表达式第三周笔记
正则引擎的分类 正则引擎的分类 正则引擎的分类主要分两种: DFA:egrep.awk.lex.flex NFA:.NET.PHP.Perl.Ruby.Python.GNU Emacs.ed.sec. ...
- jQ插件编写
参考文档:http://www.cnblogs.com/Dlonghow/p/4142034.html 编写插件最先接触到的就是jQuery.fn.extend 和jQuery.extend 这个两个 ...
- 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 ...
- this .运算符 和 [] 运算符
首先看这个 这两个运行结果是不一样的 前两个是3 后面是10 var length = 10; var arr = [function(){console.log(this.length);},2 ...
- linux线程之pthread_join和pthread_detach
在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在 被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反 ...
- zoj 3708 Density of Power Network
/*看英文和图我头都大了,不过很简单的.*/ #include<string.h> #include<stdio.h> ][],q[],w[]; int main(int ar ...
- asp.net中实现MD5加密、解密的方法
这个MD5加密.解密的方法会使用即可. 使用时的代码备忘:Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile(& ...
- myeclipse 配置weblogic 异常
java.lang.UnsupportedClassVersionError: Bad version number in .class file当前JDK与weblogic版本不匹配.
- new Date()的参数
前两天发现手机页面的倒计时在Android上正常显示,在iPhone却不能显示. 后来又发现在ff和ie里也不显示.(以前只在chrome里看过,显示正常). 后来同事改了new Date()里字符串 ...
- HTML+CSS笔记 CSS入门续集
继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代(标签). 语法: p{color:red;} <p> ...