C/C++ 基本数据类型之间的相互转换 int / char / string / vector windows与linux通用

参考:

http://blog.csdn.net/xinwang24/article/details/6612686

http://zhidao.baidu.com/link?url=or5e32M8_B32g1alI6fHMiu9e5GNeVFf4Lrp2ZhIfP3ngnXzNDNwpGCpGCP0tXH1nQqJtRECNC9BOoLbQClOZa

http://blog.sina.com.cn/s/blog_4ba5b45e0102durh.html


##################################################


int类型转换为string类型:

第一种方法;

使用头文件:

#include <strstream>

该头文件仅限于windows环境下使用

程序如下:

string int_to_string(int i)
{
    string s;
    strstream ss;
    ss<<i;
    ss>>s;

    return s;
}

第二种方法:

程序如下:

string int_to_string(int i)
{
    char ch[10];
    sprintf(ch, "%d", i);
    string s(ch);

    return s;
}

第二种方法在windows和linux环境下均可使用

同理,lf类型以及double类型转换为string类型均可使用上述方法,只需在上面程序中将int类型改为相应类型即可


##################


char类型转换为string类型

第一种方法:

string char_to_string(char ch)
{
	string s="a"; //初始化字符串s
	s[0]=ch;      //置换

	return s;
}

第二种方法:

string char_to_string(char ch)
{
	char cch[10];
	sprintf(cch, "%c", ch);
	string s(cch);

	return s;
}

个人推荐第一种方法,因为对于第二种方法,无法有效利用字符数组

以上两种方法均可在windows和linux环境下使用


##################################################


vector<string>类型转换为string类型:

	string aa="aa";
	string bb="bb";
	string cc="cc";
	string dd="ddddddddddddddddddd";
	string ee="adfadfasdf";

	vector<string> vec;
	vec.push_back(aa);
	vec.push_back(bb);
	vec.push_back(cc);
	vec.push_back(dd);
	vec.push_back(ee);

	for (int i=0; i<vec.size(); i++)
	{
		cout<<vec[i]<<endl;
	}

	string *d=&vec[0];
	for (int i=0; i<vec.size(); i++)
	{
		cout<<d[i]<<endl;
	}

	cout<<sizeof(d)<<endl;
	cout<<d->size()<<endl;
	cout<<vec.size()<<endl;
	cout<<sizeof(vec)<<endl;	

	cout<<"end..."<<endl<<endl;


linux环境下运行结果:


windows环境下运行结果:



可以简单的把vector<string>看成string数组形式

上述程序均可在windows和linux环境下使用


####################################################


char[]数组转换成string类型:

	char ss[10]="zhuj";
	string s(ss);  //方法1
	string str=(string)ss;  //方法2
	cout<<ss<<endl;
	cout<<str<<endl;

将字符数组转换成字符串有两种方法


上述程序均可在windows和linux环境下使用

########################################################


char类型数字以及char[]数组类型数字转换为int类型

char ch='3';
int i=atoi(&ch);
cout<<i<<endl;

使用函数atoi();

int atoi(const char *_Str)


char ch[4]="123";
int i=atoi(ch);
cout<<i<<endl

同理,可以使用atof函数转换char数组类型数字为double类型

double atof(const char* String)


函数atoi和atof在windows和linux环境下均可使用


##################


string类型转换为char[]数组类型

程序如下:

</pre><pre name="code" class="cpp">	string str="asdf";
	string str2="1325134";
	char *ch=new char[str.size()];
	char *ch2=new char[str2.size()];
	sprintf(ch, "%s", str.c_str());
	sprintf(ch2, "%s", str2.c_str());
	cout<<ch<<endl;
	cout<<ch2<<endl;


以上程序在windows和linux环境下均可使用


#########################################


多个数字转换为char数组类型

第一种方法


        int i=1234;
	char ch[10];
	itoa(i, ch ,10);
	cout<<ch<<endl;

函数itoa可以在windows环境下运行,但无法运行在linux环境下

参数10表示以10进制形式转换,如果为16则以16进制转换


第二种方法:

int i=1234;
char ch[10];
memset(ch, 0, 10);
sprintf(ch, "%d", i);
cout<<ch<<endl;
第二种方法在windows和linux环境下均可使用

在windows环境下推荐第一种方法,跨平台下推荐第二种方法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值