STL之string函数整理

1 篇文章 0 订阅

STL的string容器使用很多,常常忘记一些函数的用法,决定边用边整理起来。
首先使用条件是include库

string的构造

  1. string s1(“abcdefg”); //用字符串的值初始化s1
  2. string s2(s1); //拷贝构造函数生成s1的副本s2
  3. string s3(s1,index); //将s1中从index的位置后的字符串作为s3的初值(从0开始计数位置)
  4. string s4(s1,index,len); //将s1中从index的位置后且长度为len的字符串作为s4的初值
  5. string s5(n,’a’); //用n个字符a来初始化字符串s5
  6. string s6(s1.begin(),s1.begin()+6); //截取s1.begin()到s1.begin()+6的区间作为s6的初值
void creatstr()
{
    string s1("abcdefg");
    string s2(s1);
    string s3(s1,3);
    string s4(s1,3,2);
    string s5(6,'a');
    string s6(s1.begin(),s1.begin()+6);
    cout<<"s1: "<<s1<<endl<<"s2: "<<s2<<endl<<"s3: "<<s3<<endl;
    cout<<"s4: "<<s4<<endl<<"s5: "<<s5<<endl<<"s6: "<<s6<<endl;
}

int main()
{
    creatstr();

    return 0;
}

头文件没写,上面有说明。看一下运行结果吧(VS):
这里写图片描述

string的常用操作函数

  1. s.empty(): 判断string s是否为空,不为空返回0(false);
  2. s1.size()/s1.length(): 返回string的长度/字符个数;
  3. s2.insert(s2.begin()+2,’3’); 在s2的第二个位置(begin()+2)插入新的字符‘3’;标准容器的插入pushback()也支持,只是只能插在最后;
  4. string operator+:串联字符串,左操作数需为string类型,右操作数可以是string,char,常字符串;
  5. s5.append(“qwer”); 将字符串(qwer)拼接在s5的后面;
  6. s5.clear(); 清空s5容器的所有内容,用eraser()可以删除单个元素;
void basicfun()
{
    string s1("abcdefg");
    string s2("qwert");
    cout<<"s1.empty(): "<<s1.empty()<<endl<<"s1.size()/length() "<<s1.size()<<"  "<< s1.length()<<endl;
    s2.insert(s2.begin()+2,'3');
    cout<<"s2-insert:"<<s2<<endl;
    string s3=s1+s2;
    string s4=s1+"gfedbca";
    string s5=s1+"123"+"abc";
    cout<<"string+的几种结果:"<<"s3: "<<s3 <<" s4 "<<s4 <<" s5 "<<s5<<endl;
    s5.append("qwer");
    cout<<"s5--append  "<<s5<<endl;
    s5.clear();
    cout<<"s5--clear  "<<s5<<endl;
}

int main()
{
    //creatstr();
    basicfun();
    return 0;
}

这里写图片描述

string中字符处理函数

  1. s1.compare(s2); //将string s1 与 s2比较,若s1>s2,返回1;若s1小于s2,返回-1;相等返回0;
  2. operator== :s1==s2;// 比较字符串s1,s2是否相等,相等返回1,否则返回0;
  3. s2.find(‘e’);// 在s2中找到字符‘e’的位置并返回;
    s2.find(‘e’,4); //从s2第4个位置开始找到字符‘e’的位置并返回;
    s2.find(“rew”); //在s2中找到字符串“rew”的位置并返回;
    s2.rfind(‘e’); // 在s2中反向查找字符‘e’的位置并返回;
    s2.find_first_of(“eee”); //在s2中查找子串“eee”的位置返回
    s2.find_first_not_of(“wsx”); // 在s2中查找第一个不在字符串“wsx”的字符位置返回;
  4. s4.replace(5,6,”replace”); // 把s4中从位置5开始的6个字符替换为“replace”;
    s4.replace(s4.begin(),s4.begin()+4,”try”); //把s4中从begin()到begin()+4的区域替换为try;
  5. s5=s2.substr(2,4); //截取s2中位置2~4的字符赋值给s5;
    s6=s2.substr(posi); //截取从posi开始的位置后所有字符赋值给s6;
void procfun()
{
    string s1("qwer");
    string s2("qwerrewq");
    string s3("asdf");
    cout<<"compare: "<<s1.compare(s2)<<"  "<<s1.compare(s3)<<" "<<s2.compare(s2)<<endl;
    cout<<" string ==  "<<(s1==s2)<<" "<<(s1==s1)<<endl;

    cout<<"find results: "<<endl;
    cout<<s2.find('e')<<"  "<<s2.find('e',4)<<"  "<<s2.find("rew")<<endl;
    cout<<s2.rfind('e')<<"  "<<s2.find_first_of("eee")<<"  "<<s2.find_first_of("wsx")<<"  "<<s2.find_first_not_of("wsx")<<endl;

    string s4("test string");
    s4.replace(5,6,"replace");
    cout<<"replace 1 result: "<<s4<<endl;
    s4.replace(s4.begin(),s4.begin()+4,"try");
    cout<<"replace 2 result: "<<s4<<endl;

    string s5=s2.substr(2,4);
    size_t posi=s2.find('e');
    string s6=s2.substr(posi);
    cout<<"substr: "<<s5<<"  "<<s6<<endl;
}

int main()
{
    //creatstr();
    //basicfun();
    procfun();
    return 0;
}

这里写图片描述

先到这吧,还有关于string的遍历和删除,其实就是容器的操作部分。
关于string 转char 之后再专门写一篇好了~

参考:http://blog.csdn.net/lsh_2013/article/details/46728993
http://blog.csdn.net/y990041769/article/details/8763366

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值