C/C++基本データ型間の相互変換int/char/string/vectorwindowsと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タイプに変換するには:
第1の方法;
ヘッダファイルを使用:
#include <strstream>

このヘッダファイルはwindows環境でのみ使用できます
手順は次のとおりです.
string int_to_string(int i)
{
    string s;
    strstream ss;
    ss<<i;
    ss>>s;

    return s;
}

2つ目の方法:
手順は次のとおりです.
string int_to_string(int i)
{
    char ch[10];
    sprintf(ch, "%d", i);
    string s(ch);

    return s;
}

第2の方法はwindowsとlinux環境で使用できます
同様に、lfタイプおよびdoubleタイプをstringタイプに変換するには、上記の手順でintタイプを対応するタイプに変更するだけで、上記の方法を使用できます.
##################
charタイプをstringタイプに変換
1つ目の方法:
string char_to_string(char ch)
{
	string s="a"; //      s
	s[0]=ch;      //  

	return s;
}

2つ目の方法:
string char_to_string(char ch)
{
	char cch[10];
	sprintf(cch, "%c", ch);
	string s(cch);

	return s;
}

個人的には、2つ目の方法では文字配列を有効に利用できないため、1つ目の方法をお勧めします.
以上の2つの方法はwindowsとlinux環境で使用できます.
##################################################
vectorタイプから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環境での実行結果:
C/C++ 基本数据类型之间的相互转换 int / char / string / vector windows与linux通用_第1张图片
Windows環境での実行結果:
C/C++ 基本数据类型之间的相互转换 int / char / string / vector windows与linux通用_第2张图片
vectorをstring配列形式と簡単に見ることができます
上記のプログラムはwindowsとlinux環境で使用できます.
####################################################
char[]配列をstringタイプに変換するには:
	char ss[10]="zhuj";
	string s(ss);  //  1
	string str=(string)ss;  //  2
	cout<<ss<<endl;
	cout<<str<<endl;

文字配列を文字列に変換するには2つの方法があります.
上記のプログラムは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進数変換を表す
2つ目の方法:
int i=1234;
char ch[10];
memset(ch, 0, 10);
sprintf(ch, "%d", i);
cout<<ch<<endl;
第2の方法はwindowsとlinux環境で使用できます.
Windows環境では第1の方法を推奨し、プラットフォームをまたいで第2の方法を推奨します.