STL string使用方法

//STL string
#include <vector>
#include <string>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
using namespace std;
string convertToString (double x)
//C++方法 :将数值转换为 string
{
    ostringstream o;
    if(o << x) return o.str();
    return "conversion error";//if error
}
double convertFromString(const string &s)
//C++方法 : 将string转换为数值
{
    istringstream i(s);
    double x;
    if(i >> x ) return x;
    return 0.0; //if error
}
int main(void)
{
    string S;//创建一个string对象
    cout<<S.length()<<endl;//length()返回string的长度
    cout<<S.empty()<<endl; //empty() 判断string是否为空
//-----------------给string对象赋值------------------//

    //1.直接赋值法
    S="Hello C++STL.";

    //2.更常用:把字符指针赋给一个string对象
    char s[100];scanf("%s",s);
    S=s;cout<<S<<endl;
//-----------------从string对象尾部添加字符------------------//
//采用“+”操作符
    S="Hello C++STL.";
    S=S+'a';cout<<S<<endl;
//-----------------从string对象尾部添加字符串------------------//

    //1.采用“+”操作符
    S="HEllO";S+="   lalala";
    cout<<S<<endl;

    //2.采用append()的方法
    S.append("abc");
    cout<<S<<endl;
//-----------------给string对象插入字符------------------//
    S="123456";
    string::iterator it;//定义迭代器
    it = S.begin();
    //把字符'A'插入到第1号位前
    S.insert(it+1,'A');
    cout<<S<<endl;
//-----------------访问string对象的元素------------------//
//其基本元素为char型  可以直接下标访问
    S="abcde123456";
    cout<<S[0]<<endl;
    cout<<S[0]-'a'<<endl;
//-----------------删除string对象的元素------------------//
    //1.清空一个字符串//直接给它赋值空字符串
        S="1234";
        S="";
        cout<<S.size()<<endl;
    //2.使用erase()方法删除迭代器所指的那个元素或一个区间中所有元素
        S="012345678910";
        //a.删除单个元素
        S.erase(it+3);cout<<S<<endl;
        //b.删除区间内的所有元素
        S="012345678910";
        S.erase(it+1,it+7);cout<<S<<endl;//左闭右开
//-----------------替换string对象的字符------------------//
//使用replace()的方法可以很方便地替换string对象中的字符,replace()的方法的重载函数很多
    S="abc123456";
    //从第3个开始,将连续的3个字符串替换为"good"
    //即将"123"替换为"good"
    S.replace(3,3,"good");
    cout<<S<<endl;
//-----------------搜索string对象的元素或子串------------------//
//采用find() //若查到返回第一个下标值,查不到返回2^32-1
    S="cat dog cat";
    //查找第一个字符'c'
    cout<<S.find('c')<<endl;
    //查找第一个字符串"dog"
    cout<<S.find("dog")<<endl;
    //查找第一个字符串"apple"
    cout<<S.find("apple")<<endl;
//-----------------string对象的比较------------------//
//使用compare()方法与其他字符串比较,同长如果它比对方大返回1,小返回-1,相等返回0
    S="cat dog cat";
    cout<<S.compare("cat dog ")<<endl;  //!!!!!!!!!!!!!!!!!!!!!!!!目前测试显示长度差
//-----------------用reverse反向排序string对象---------//
    S="cat dog cat";
    reverse(S.begin(),S.end());
    cout<<S<<endl;
//----------------string对象作为vector元素---------//

    vector<string> v;
    v.push_back("JACK");
    v.push_back("TOM");
    cout<<v[1]<<endl;
    cout<<v[1][0]<<endl;//打印字符
    cout<<v[1].length()<<endl;//显示长度
    cout<<v.size()<<endl;
//----------------string对象与sscanf函数---------//
//sscanf()函数可以把一个字符串按需要的方式分离出子串,甚至是数字
    string s1,s2,s3;
    char sa[100],sb[100],sc[100];
    sscanf("abc 123 pc","%s %s %s",sa,sb,sc);
    s1=sa;s2=sb;s3=sc;
    cout<<s1<<s2<<s3<<endl;
    //同样适用于字符串式赋值
    int a,b,c;sscanf("1 2#3","%d %d#%d",&a,&b,&c);//同样'#'只要检索符一致即可
    cout<<a<<" "<<b<<" "<<c<<endl;
//----------------string对象与数值相互转换--------//
//#include <sstream> 并且定义了函数见上面

    //C++方法 :将数值转换为 string 的第一种方法
    char B[10];string e;
    sprintf(B,"%d",1975);
    e=B;cout<<e<<endl;
    //C++方法 : 将数值转换为 string 的第二种方法
    string cc=convertToString(1976);
    cout<<cc<<endl;
    //C++ 方法 : 将string 转换成数值
    string dd="2006";
    int p=convertFromString(dd)+2;
    cout<<p<<endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值