给自己总结个关于string类的笔记,方便以后查看。
一、string对象的定义,初始化及读写
string s1;//默认构造函数,s1为空串
string s2(s1);//将s2初始化为s1的副本
string s1('wuyang');//将s1初始化为一个字符串面值副本
string s1(n,'w');//将s1初始化为'w'的n个副本
cin>>s1;//读取有效字符,遇到空格结束
getline(cin,s1);//读取字符直至遇到换行(可以读取空格)
getline(cin,s1,'w');//读取字符直至遇到字符'w'(可读取空格和换行)
二、string对象的操作
s1.empty();//s1为空返回真,否则返回假
reverse(s1.begin(),s1.end());//将string对象迭代器指向的一段区间内元素反向排序
s1.size();//返回字符个数
s1.length();//同上,返回字符个数,一般不怎么用
s1[n];//返回位置为n的字符(从0开始计数)
s1.append("abc");//向字符串尾部添加字符串
s1+s2;//连接s1,s2两个字符串
/*可以把一个string对象和一个字符面值相连,但不能将两个字符面值相加。
s1+'w'+'y'这种相加是允许的。'w'+'y'+s1是错误的*/
s1=s2;//替换
s1==s2;//判断相等,相等返回真,否则返回假
str2.size()返回的值并不是int类型
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1;
string str2("the size of ");
string str3=" hello world ";//空格不会忽略
str3+=str2;
str3.append("haha secessful");
cout<<str3<<endl;
cout<<"the size of is "<<str2.size()<<endl;
/*注意这里取长度的str2.size(),和str2.length(),但是注意str2.size()返回的值并不是int类型,
事实表明size_type存储的string长度是int所能存储的两倍*/
getline(cin,str1); //read line at time until end-of-file,注意写法。
while(!str1.empty()) //返回一个bool值,空的话返回true,否则返回false。
{
for(string::size_type i=0; i!=str1.size(); ++i) //注意size_type类型
{
cout<<str1[i];
}
cout<<endl;
break;
}
return 0;
}
三、string对象中字符的处理(头文件#include”cctype”)
isalnum(c);//如果c是字母或数字,返回真
isdigit(c);//如果c是数字,返回真
isalpha(c);//如果c是字母,返回真
islower(c);//如果c是小写字母,返回真
isupper(c);//如果c是大写字母,返回真
tolower(c);//如果c是大写字母,则返回其小写字母,否则返回c
toupper(c);//如果c是小写字母,则返回其大写字母,否则返回c
isgraph(c);//如果c是可打印字符且c不是空格,返回真
isprint(c);//如果c是可打印字符(c可以是空格),返回真
iscntrl(c);//如果c是控制符,返回真
isdigit(c);//如果c是数组,返回真
ispunct(c);//如果c是标点符号,返回真
isxdigit(c);//如果c是十六进制数,返回真
isspace(c);//如果c是空格字符,返回真
//空格字符包括空格' ',水平定位字符'\t',归位键'\r',换行'\n',垂直定位字符'\v',翻页'\f'
#include<iostream>
#include<string>
#include<cctype>//注意string字符操作需要增加头文件
using namespace std;
int main()
{
string str1="Hello World!!!";
string::size_type punct_cnt = 0;
for(string::size_type i=0;i!=str1.size();++i)
{
if(ispunct(str1[i]))
++punct_cnt;
str1[i]=toupper(str1[i]);
}
cout<<"字符中标点符号有:"<<punct_cnt<<endl;
cout<<str1<<endl;
return 0;
}
四、string与数值的相互转换(可以直接当做模板)
#include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
#include <vector>
#include <cstdio>
#include <sstream>
using namespace std;
//c++方法:将数值转换为string
string convert_to_string(double x)
{
ostringstream o;
if(o<<x)
return o.str();
return "conversion error";
}
//c++方法,将string转化为数值
double convert_from_string(const string &s)
{
istringstream i(s);
double x;
if(i >> x)
return x;
return 0.0;
}
int main(int argc,char *argv[])
{
//将数值转换为string的第一种方法:c方法
char b[10];
string a;
sprintf(b,"%d",1975); //数值转化为string
a=b;
cout<<a<<endl;
string cc=convert_to_string(1976);
cout<<cc<<endl;
string dd="115165";
int p=convert_from_string(dd)+2;
cout<<p<<endl;
return 0;
}
五、字符的插入,删除,查找
插入
string s1;
string::iterator it;//定义迭代器
it=s1.begin();//获取迭代器位置,此处为字符串首
s1.insert(it+1,'p');//字符'p'插到第一个字符前(字符位置从零计数)
删除
string s1;
string::iterator it;//定义迭代器
it=s1.begin();//获取迭代器位置,此处为字符串首
s1.erase(it+3);//删除从it起第三个元素(字符位置从零计数)
s1.erase(it,it+4);//删除it到it+4间四个元素
替换
string s1;
s1.replace(pos,num,char*c);//替换从下标pos开始的num个元素为*c所指的元素
替换操作的样例
//头文件略
int main()
{
string s1="abc123456";
s1.replace(3,3,"good");//从下标3开始连续三个元素替换为good
cout<<s1<<endl;
return 0;
}
//输出结果abcgood456
查找
string s1;//find函数有很多,这里只列举了一点点
s1.find('c');//返回第一个字符'c'的下标,查不到返回4294967295,即string::npos
s1.find("c");//返回第一个子串"c"的下标,查不到返回4294967295,即string::npos
s1.find("good");//返回第一个子串"good"的下标,查不到返回4294967295
s1.find('c',pos);//从下标pos开始,返回第一个字符'c'的位置
s1.find("c",pos);//从下标pos开始,返回第一个子串"c"的位置
s1.find("good",pos,num);//从下标pos开始,返回子串中前num个子串在s1中的位置
s1.rfind('c',pos);//从pos开始从后往前查找字符'c'在s1中的下标
s1.rfind("c",pos);//从pos开始从后往前查找子串'c'在s1中的下标
s1.rfind("good",pos,num);//从下标pos开始,从前往后查找,返回子串中前num个子串在s1中的位置
接下来还有map,list,vector等容器。。。