c++里如何重定向一个字符串 使string变成int型 请大神指导啊

c++里如何重定向一个字符串 使string变成int型 请大神指导啊

用atoi就 可以了
比如:
string s="534";
int f=atoi(s.c_str());
cout<<f; //printf("f=%d",f);

include 使用atoi就行

包含一下stdlib.h

vs2008以上好像昂要用_wtoi

int转string
int n = 0;
std::stringstream ss;
std::string str;
ss< ss>>str;
string转int
std::string str = "123";
int n = atoi(str.c_str());
复制代码
#include "stdafx.h"

#include
#include

using namespace std;
void main()
{
// int 转 string
stringstream ss;
int n = 123;
string str;
ss< ss>>str;
// string 转 int
str = "456";
n = atoi(str.c_str());
}

ss< ss>>str; 改为 ss<>str;

用atoi就 可以了
比如:
string s="534";
int f=atoi(s.c_str());
cout<<f; //printf("f=%d",f);

用atoi即可,如果是wstring,就用wtoi