[C++ 从入门到放弃-02]C++STL之string

C++STL提供了string基本字符串系列容器来处理字符串,可以把string理解成字符串类也可以。慢慢掌握之后你会发现用起来十分的方便。

string提供了添加、删除、替换、查找和比较等等很多的方法。

[string对象的赋值]
1. 直接给字符串对象赋值
2. 用字符指针给字符串对象赋值

#include<iostream>  
#include<string>   
#include<stdio.h>  
using namespace std;  

int main()  
{  
    string s;  
    //直接给对象赋值   
    s = "Hello, C++STL.";  
    cout<<s<<endl;  

    char ss[5000];  
    //scanf()的输入速度要比cin快得多  
    //scanf()是c语言的  
    scanf("%s", &ss);  
    //把整个字符数组赋给string对象  
    s = ss;  
    //输出字符对象  
    cout<<s<<endl;   
    return 0;  
}  

[string对象尾部添加字符]
如果你看了之前写的vector,其实string的一些方法和vector是一样的。

在string对象的尾部添加一个字符char,用+操作符就可以了。

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s;  
    s = s + 'a';  
    s = s + 'b';  
    s = s + 'c';  
    cout<<s<<endl;  
    return 0;   
}  

[两个string对象的连接]
1. 直接用+操作符
2. 采用append()方法

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s;  
    s = s + "abc";  
    s = s + "123";  
    cout<<s<<endl;  

    string ss;  
    ss.append("abc");  
    ss.append("123");  
    cout<<ss<<endl;  

    return 0;  
}  

[string对象的插入字符操作]

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s;  
    s = "123456";  
    //定义迭代器  
    string::iterator it;  
    //迭代器位置初始化位置为字符串首地址  
    it = s.begin();  

    //把字符'p'插入到第1个字符位置前,字符位置是从0开始计数   
    s.insert(it+1, 'p') ;  
    cout<<s<<endl;  
    return 0;  
}  

[访问string对象的元素]
采用下标的方式进行访问,下标是从0开始计数的,string对象的元素是一个个的字符,即char

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s;  
    s = "abc123456";  
    //输出字符串的首元素  
    cout<<s[0]<<endl;  
    return 0;  
}  

[删除string对象元素]
1. 清空一个字符串string对象,可以直接给它赋值一个空字符串就可以了
2. 使用erase()方法,可以删除一个元素或者一段区间的元素

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s;  
    s = "abc123456";  

    string::iterator it = s.begin();  

    s.erase(it+3);  
    cout<<s<<endl;  

    s.erase(it, it+4);  
    cout<<s<<endl;  

    return 0;  
}  

[在string对象中查找元素或者查找子串]
采用find()方法,查找字符串中的第一个字符元素或者子串。查找到返回下标值,查找不到返回4294967295

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s = "cat dog cat";  
    //查找第一个字符'c',返回下标值  
    cout<<s.find('c')<<endl;  

    //查找第一个子串'cat',返回下标值  
    cout<<s.find("cat")<<endl;  

    //查找第一个子串"STL",返回下标值,查不到,返回一个最大值4294967295  
    cout<<s.find("STL")<<endl;  
    return 0;   
}  

[string的替换操作]
采用replace()方法,replace()的重载函数有很多个,常用的就一两个。

#include<iostream>  
#include<string>   
using namespace std;  

int main()  
{  
    string s = "abc123456";  
    //从第3个字符开始,将连续的3个字符替换成"good" ,此时不是从0开始计数   
    s.replace(3, 3, "good");  
    cout<<s<<endl;  
    return 0;  
}  

[string的反向排列操作]

#include<iostream>  
#include<string>  
#include<algorithm>  
using namespace std;  

int main()  
{  
    string s = "123456789";  
    reverse(s.begin(), s.end());  
    cout<<s<<endl;  
    return 0;  
}  

[string对象的比较]
string对象可以使用compare()方法与其他字符串想比较。假设字符串A和B,A如果比B大,则返回1,如果A比B小,则返回-1,如果A和B相同,则返回0

#include<iostream>  
#include<string>  
using namespace std;  

int main()   
{  
    string s = "cat dog cat";  

    //s比"cat"字符串大,返回1  
    cout<<s.compare("cat dog")<<endl;  

    //s与"cat dog cat"一样,返回0  
    cout<<s.compare("cat dog cat")<<endl;  

    //s比"dog"字符串小,返回-1  
    cout<<s.compare("dog")<<endl;  
    return 0;  
}  

[string对象作为vector元素]
类似于字符串数组

#include<iostream>  
#include<string>  
#include<vector>  
#include<algorithm>  
using namespace std;  

int main()  
{  
    vector<string> v;  
    v.push_back("CaiCai");  
    v.push_back("At");  
    v.push_back("Ningbo");  

    cout<<v[0]<<endl;  
    cout<<v[1]<<endl;  

    cout<<v[0][0]<<endl;  
    cout<<v[1][1]<<endl;  

    cout<<v[2].length()<<endl;  

    return 0;  
}  

[string对象的数字化处理]
一些string是纯数字的,高精度的数据,需要把每位分离出来,我们可以把高精度数据当做字符串处理。

#include<iostream>  
#include<string>  
using namespace std;  

int main()  
{  
    string s = "1234059";  
    int i, sum = 0;  
    for(i = 0; i < s.length(); i ++)  
    {  
        if(s[i] == '0')  
            sum += 0;  
        else if(s[i] == '1')  
            sum += 1;  
        else if(s[i] == '2')  
            sum += 2;  
        else if(s[i] == '3')  
            sum += 3;  
        else if(s[i] == '4')  
            sum += 4;  
        else if(s[i] == '5')   
            sum += 5;  
        else if(s[i] == '6')  
            sum += 6;  
        else if(s[i] == '7')  
            sum += 7;  
        else if(s[i] == '8')  
            sum += 8;  
        else if(s[i] == '9')  
            sum += 9;  
    }  
    cout<<sum<<endl;  
    return 0;  
}  

[string对象与字符数组的相互操作]
下面这段代码来说明string与字符数组的相互转化和区别

#include<iostream>  
#include<stdio.h>  
#include<string>  
using namespace std;  

int main()  
{  
    string s;  
    char ss[100];  

    //输入字符串到字符数组中  
    scanf("%s", &ss);  
    //字符数组赋值给字符串对象  
    s = ss;  

    //用printf输出字符串对象,要采用c_str()方法  
    printf(s.c_str());   
    cout<<endl;  

    //用printf输出字符数组  
    printf("%s", ss);  
    cout<<endl;  

    cout<<s<<endl;  
    cout<<ss<<endl;  
    return 0;  
}  

[string对象与sscanf函数]
sscanf()很有用,可以把一个字符串按照你需要的方式分离成你想要的子串。

#include<iostream>  
#include<cstring>  
#include<stdio.h>  
using namespace std;  
int main()  
{  
    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("123 456 789", "%d %d %d", &a, &b, &c);  
    cout<<a<<" "<<b<<" "<<c<<endl;  

    //将字符串分离成数字,分隔符为 ,和$  
    int x, y, z;  
    sscanf("4,5$6", "%d,%d$%d", &x, &y, &z);  
    cout<<x<<" "<<y<<" "<<z<<endl;  

    return 0;   
}  

[string对象与数值相互转换]
string对象与数值之间需要相互转换

#include<iostream>  
#include<string>  
#include<sstream>  
#include<stdio.h>  
using namespace std;  

//c++方法:将数值转换为string  
string convertToString(double x)  
{  
    ostringstream O;  
    if(O << x)  
        return O.str();  
    return "conversion error";  
}   

//c++方法:将string转换成数值  
double convertFromString(const string& s)  
{  
    istringstream i(s);  
    double x;  
    if(i >> x)  
        return x;  
    return 0.0;  
}   

int main()  
{  
    char b[10];  
    string a;  
    sprintf(b, "%d", 1975);  
    a = b;  
    cout<<a<<endl;  

    //将数值转换成string的第二种方法:C++方法  
    string cc = convertToString(1976);  
    cout<<cc<<endl;  

    //将string转换为数值的方法:C++方法  
    string dd = "2015";  
    int p = convertFromString(dd) + 2;  
    cout<<p<<endl;  

    return 0;     
}  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值