int to string

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    char c[100];
    int m=199;
    itoa(m,c,10);
    s=c;
    s.insert(0,"zhang");
    cout<<s<<endl;
    return 0;


}

string to int

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s="56sd";
    int a=atoi(s.c_str());
    cout<<a<<endl;
    return 0;
}

 

 

另外一种方法,包含#include<sstream>,例如

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
	stringstream s;
	string ss="234";
	s<<ss;
	s<<"abcd";


	string s0;
	int n;
	s>>n;
	s>>s0;

	cout<<n<<"  "<<s0<<endl;

	return 0;
}

  

posted on 2015-05-16 23:26  三人纷纷  阅读(1379)  评论(6编辑  收藏  举报