string基本字符系列容器(二)

string对象作为vector元素

string对象可以作为vector向量元素,这种用法类似字符串数组。

#include<string>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
        vector<string> vec;
        vec.push_back("qianshou");
        vec.push_back("Iwas shocked by your inability");
        cout<<vec[0]<<endl;
        cout<<vec[0][2]<<vec[0][3]<<endl;
        cout<<vec[1]<<endl;
        return 0;
}

string数组可以看做是一个二维的字符数组


string类型的数字化处理

我们通常会遇到将一个整数的每个位分离出来的情况,一般的方法是采用不断取余获得余数的方法,但是当数字较大的时候,计算量就比较大,这是我们可以使用string对象来处理:

#include<string>
#include<iostream>
using namespace std;
int main()
{
        string num = "1234506";
        int len = num.length();
        //分别输出各位的数字
        int b=0;
        for(int i=0;i<len;i++)
        {
               b= num[i]-'0';
               cout<<b<<"";
        }
        //求得各位数字的和
        int sum=0;
        for(int i=0;i<len;i++)
        {
               sum+= num[i]-'0';
        }
        cout<<"\nsum:"<<sum<<endl;
        return 0;
}

输出结果:

1 2 3 4 5 0 6
sum:21


string对象与字符数组相互操作

可以直接把字符数组的首地址传给string对象,达到复制字符数组的效果;string对象也可以转化为char数组,不过只能赋值给静态的字符指针:

#include<string>
#include<iostream>
using namespace std;
int main()
{
        charc[] = "hello world";
        string str;
       
        str = c;       //将字符串指针赋给string变量
        cout<<str<<endl;
        str[1]= 'x';
        cout<<str[0]<<str[1]<<str[2]<<endl;
       
        const char *p; //只能使用常指针只接受指向string的指针
        p= str.c_str();
        cout<<p<<endl;
        cout<<p[2]<<endl;
        return 0;
}

输出结果:

hello world
hxl
hxllo world
l

string对象与sscanf函数

sscanf函数很管用,它可以把一个字符串按照你需要的方式分离出子串,甚至是数字。

#include<string>
#include<iostream>
using namespace std;
int main()
{
        string s1,s2,s3;
        char sa[100],sb[100],sc[100];
        //将字符串分离成子串,分隔符为空格
        sscanf("abc 123 xyz","%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;
        return 0;
}

输出结果:

abc 123 xyz
123 456 789

string对象与数值相互转换

 方法一:

使用atoi、atol、atof、strtod、strtol、strtoul、itoa() 、fcvt()等库函数进行过转换,在使用该库函数的时候需要引入头文件cstdlib。

具体用法详见:

http://blog.csdn.net/tsinfeng/article/details/5844838

http://see.xidian.edu.cn/cpp/html/1573.html

因为以上库函数的参数是字符指针,所以对于string对象而言,需要先使用c_str方法,将字符数组的首指针取出,然后才可以转化为对应数值。

浮点数转字符串的时候,并不是直接得到对应的浮点数,而是分别得到一串数字p,小数点的位置dec_pl以及正负号sign。

#include<string>
#include<cstdlib>
#include<iostream>
using namespace std;
int main()
{
    string s1 = "123";
	string s2 ="25.3";
	int a1;double a2;
	
	//string类型转化为数值 
	a1 = atoi(s1.c_str()); 
	a2 = atof(s2.c_str());
	cout<<a1<<" "<<a2<<endl;
    
    //数值转化为string对象
	a1 = 25;
	a2 = 3.1415; 
	char *p;
	itoa(a1,p,10);
	s1 = p;
	int dec_pl,sign,ndigit=4;
	p = fcvt(a2,ndigit,&dec_pl,&sign);
	s2 = p;
	string t1 = s2.substr(0,dec_pl);
	string t2 = s2.substr(dec_pl);
	s2 = t1+'.'+t2;
	cout<<s1<<" "<<s2;
	return 0;
}
输出结果:

123 25.3
25 3.1415

 方法二:

使用sscanf将string转换为数字,使用printf将数字转换为string。

#include<string>
#include<iostream>
using namespace std;
int main()
{
	string s1,s2;
	
	//将字符串分割成数字,分隔符为空格 
	int a; double b;
	s1 = "1234 3.1415";
	sscanf(s1.c_str(),"%d %lf",&a,&b);
	cout<<a<<" "<<b<<endl;
	
	//将数字转化为字符串
	char p[10];
	int c = 3456;
	double d = 2.345;
	sprintf(p,"%d",c);
	s1 = p;
	sprintf(p,"%lf",d);
	s2 = p;
	cout<<s1<<" "<<s2<<endl;
	return 0;
}

输出结果:

1234 3.1415
3456 2.345000




posted @ 2014-04-11 09:33  千手宇智波  阅读(198)  评论(0编辑  收藏  举报