C++中stoi函数

作用:

  将 n 进制的字符串转化为十进制

头文件:

#include <string>

用法:

1 stoi(字符串,起始位置,n进制),将 n 进制的字符串转化为十进制
2 
3  示例:
4 stoi(str, 0, 2); //将字符串 str 从 0 位置开始到末尾的 2 进制转换为十进制

但好像不是标准函数,慎用吧。

案例:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string str = "1010";
 9     int a = stoi(str, 0, 2);
10     cout << a << endl;
11         return 0;
12 }        

输出结果:

10

 参考:

http://www.cplusplus.com/reference/string/stoi/

posted @ 2019-08-14 14:45  Anzer  阅读(49438)  评论(3编辑  收藏  举报