C++中各种数据类型之间的转换:

1.int 转 string:

    1.使用sstream

    编程:
                #include <sstream>
                #include <string>
                #include <iostream>

                using namespace std;


                int main()
                {

                    int m;

                    stringstream ss;


                    cin>>m;


                    string str;

                    ss<<m;
                    ss>>str;


                    cout<<str<<endl;


                    return 0;
                }

注意stringstream 流只能单次使用,
即一次只能将一个int变量输入转为
string变量输出,不可以重复使用。

    2.C++11中的库函数to_string()

            1.c语言<stdlib.h>

            编程:

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

                int main()
                {
                    string str="234";
                    int n=atoi(str.c_str());
                    cout<<n<<endl;
                }

语法:

                const char *c_str();
                c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 
                这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
                注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针 

比如:最好不要这样:
char* c;
string s=”1234”;
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理

应该这样用:
char c[20];
string s=”1234”;
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作

再举个例子
c_str() 以 char* 形式传回 string 内含字符串
如果一个函数要求char*参数,可以使用c_str()方法:
string s = “Hello World!”;
printf(“%s”, s.c_str()); //输出 “Hello World!”

            2.C++11中的stoi()


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

                    int main(){
                        string str="234";
                        int n=stoi(str);
                        cout<<n<<endl;
                        return 0;
                    }


                    注意,因为是c++11的标准
                    编译时候的格式:
                    g++ test.cpp -o test -std=c++11

2.各种基本数据类型
int /long /long long / unsigned /unsigned long/unsigned long long/
/float /double/long double

转为string 类型。

        1.std命令空间下有一个C++标准库函数std::to_string(),
        可用于将数值类型转换为string。使用时需要include头文件<string>。





        函数原型申明如下:
string to_string (int val);
            string to_string (long val);
            string to_string (long long val);
            string to_string (unsigned val);
            string to_string (unsigned long val);
            string to_string (unsigned long long val);
            string to_string (float val);
            string to_string (double val);
            string to_string (long double val);     


编程:


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

                int main(){

                    string str;
                    int n=123;

                    str=to_string(n);


                    cout<<str<<endl;
                    return 0;
                }


注意,这个也是c++11标准

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

            int main(){

                string str;
                double n=123.233;

                str=to_string(n);




                cout<<setfill('*')<<setw(20)<<str<<endl;
                return 0;
            }

setw(20)对其后面的str变量起作用,也就是设置str输出的时候占据20字节,
setfill(‘‘)将前面的空格以‘’填充。配合使用。

都需要包含头文件 “#include ”

        2.使用c标准函数库
            包含头文件  #include <stdlib.h>

具体函数:

    string -> int : atoi(str.c_str())  

                    string -> unsigned int : strtoul(str.c_str(),NULL,10)

                    string ->long long int: atoi(str.c_str())


                    string ->unsigned long long int : strtoull(str.c_str(),NULL,0)


                    string -> float / double : atof(str.c_str())


                    string -> long double : strtold(str.c_str())



        3.使用c++11标准中引入的c++库函数:
string str;

                    stoi(str)

                    stol(str)

                    stoul(str)

                    stoll(str)

                    stoull(str)

                    stof(str)

                    stod(str)

                    stold(str)


总结,一般的话,用C++11标准中的 to_string()函数 和对应的 stoi() 等函数

不能用c++11标准的时候,用str.c_str() 结合atoi() 、atof() 函数来使用。

string 转其他类型: 即int n=atoi(str.c_str()) ; double db=atof(str.c_str());

其他类型转string:

最后,C++中字符数组和string 类型如何相互转化呢

    1.字符数组转化为string 类型:


        编程:
#include<iostream>
                #include<string>

                using namespace std;

                int main(){

                    string str;

                    char ch[]="abcdefg";

                    str=ch;




                    cout<<str<<endl;
                    return 0;
                }           

如上,把字符数组直接当成string 类型的变量来操作即可。

    2.string 类型转化为字符数组类型:

编程:

#include<iostream>
                #include<string>
                #include <cstring>

                using namespace std;

                int main(){

                    string str("abcdefg");

                    char buf[10];


                    strcpy(buf,str.c_str());



                    cout<<buf[5]<<endl;
                    return 0;
                }           

如上,注意,需要包含头文件#include