Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串数字如何转换为整数? #包括 使用名称空间std; int main(){ string a=“1234”;//此字符串如何转换为整数 系统(“暂停”); 返回退出成功; }_C++ - Fatal编程技术网

C++ 字符串数字如何转换为整数? #包括 使用名称空间std; int main(){ string a=“1234”;//此字符串如何转换为整数 系统(“暂停”); 返回退出成功; }

C++ 字符串数字如何转换为整数? #包括 使用名称空间std; int main(){ string a=“1234”;//此字符串如何转换为整数 系统(“暂停”); 返回退出成功; },c++,C++,字符串a=“1234”; 如何将其转换为整数如果您有C++11及更高版本,请使用 #include <iostream> using namespace std; int main() { string a = "1234"; //How this string convert in integer number system("pause"); return EXIT_SUCCESS; } (在C++11之前,您可以使用std::strol;)您可以使用将s

字符串a=“1234”;
如何将其转换为整数

如果您有C++11及更高版本,请使用

#include <iostream>
using namespace std;

int main() {
   string a = "1234"; //How this string convert in integer number

   system("pause");
   return EXIT_SUCCESS;
}
(在C++11之前,您可以使用
std::strol;

您可以使用将
std::string
转换为
int

int n = std::stoi(a);
#包括
#包括
int main(){
std::string a=“1234”//此字符串如何转换为整数
int b=标准::stoi(a);
系统(“暂停”);
返回退出成功;
}

您必须使用std::stoi:

#include <iostream>
#include <string>

int main() {
   std::string a = "1234"; //How this string convert in integer number
   int b = std::stoi(a);
   system("pause");
   return EXIT_SUCCESS;
}
#包括
#包括
std::string s=“123”;
整数=标准::stoi(s);

你可以使用助推器

#include <iostream>
#include <string>

std::string s = "123";
int number= std::stoi(s);
#包括
std::string str_num=“12345”;
int值=0;
尝试
{
value=boost::词法转换(str\u num);
}
捕获(boost::错误的\u词法\u cast&)
{
//转换错误-调用代码将处理
}

这种方法可以很容易地修改代码来处理浮点或加倍,如果字符串包含这些类型的数值也

< p> C++标准有一个特殊的函数

#include <boost/lexical_cast.hpp>

std::string str_num = "12345";
int value = 0;
try
{
    value = boost::lexical_cast<int>(str_num);
}
catch(boost::bad_lexical_cast &)
{
    // error with conversion - calling code will deal with
}

也许你可以试试这个

int stoi(const string& str, size_t *idx = 0, int base = 10);

请参阅或您可以搜索stoi函数,并查看如何使用它。也许它可以工作,但永远不要尝试,因为我没有读过c++编译器。

我不建议使用atoi,而是使用。@JoachimPileborg:Good point;如果我修改一下,你介意吗?对一个短范围的数字使用stoi,但是如果你有一个长的数字,它就无法执行转换。当你尝试来回解析模板时,这种方法确实是优越的。
string a = "28787" ;
int myNumber;
istringstream ( a) >> myNumber;