这篇文章主要介绍“C++中的stoi/stol/stoll函数怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++中的stoi/stol/stoll函数怎么使用”文章能帮助大家解决问题。
#include <string>
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
int stoi(const std::wstring& str, std::size_t* pos = 0, int base = 10);
功能:将字符串str转成 有符号 int 整数
参数:
str:字符串
pos:存储将字符串str转成有符号整数,处理了str中字符的个数的地址,默认为NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str是 0 开头为八进制,str是 0x 或 0X 开头是十六进制,默认为十进制
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { int a; size_t pos = 0; string str; str = "-1235"; a = stoi(str); cout << "a = " << a << endl; //a = -1235 str = "1235"; a = stoi(str); cout << "a = " << a << endl; //a = 1235 str = " -12 35"; a = stoi(str, &pos); //会舍弃空白符 cout << "a = " << a << endl; //a = -12 cout << "pos = " << pos << endl; //pos = 5 str = " -12ab35"; a = stoi(str, &pos); cout << "a = " << a << endl; //a = -12 cout << "pos = " << pos << endl; //pos = 5 str = "0123"; a = stoi(str); cout << "a = " << a << endl; //a = 123 str = "0x123"; a = stoi(str); cout << "a = " << a << endl; //a = 0 return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { int a; size_t pos = 0; string str; str = "0x123"; a = stoi(str, NULL, 16); //base = 16,指定十六进制 cout << "a = " << a << endl; //a = 291 str = "0x123"; a = stoi(str, NULL, 0); //base = 0,自动检测数值进制 cout << "a = " << a << endl; //a = 291 str = "-12"; a = stoi(str, &pos, 16); //-(2 + 1*16) cout << "a = " << a << endl; //a = -18 cout << "pos = " << pos << endl; //pos = 3 str = "12"; a = stoi(str, &pos, 16); //2 + 1*16 cout << "a = " << a << endl; //a = 18 cout << "pos = " << pos << endl; //pos = 2 str = " -12 35"; a = stoi(str, &pos, 16); //会舍弃空白符 cout << "a = " << a << endl; //a = -18 cout << "pos = " << pos << endl; //pos = 5 str = " -ab"; a = stoi(str, &pos, 16); //-(11 + 10*16) cout << "a = " << a << endl; //a = -171 cout << "pos = " << pos << endl; //pos = 5 str = "0123"; a = stoi(str, NULL, 16); //(3 + 2*16 + 1*16*16) cout << "a = " << a << endl; //a = 291 return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { int a; size_t pos = 0; string str; str = "0x123"; a = stoi(str, NULL, 8); //base = 8,指定八进制 cout << "a = " << a << endl; //a = 0 str = "0123"; //(3 + 2*8 + 1*8*8) a = stoi(str, NULL, 0); //base = 0,自动检测数值进制 cout << "a = " << a << endl; //a = 83 str = "-12"; a = stoi(str, &pos, 8); //-(2 + 1*8) cout << "a = " << a << endl; //a = -10 cout << "pos = " << pos << endl; //pos = 3 str = "12"; a = stoi(str, &pos, 8); //2 + 1*8 cout << "a = " << a << endl; //a = 10 cout << "pos = " << pos << endl; //pos = 2 str = " -12 35"; a = stoi(str, &pos, 8); //会舍弃空白符 cout << "a = " << a << endl; //a = -10 cout << "pos = " << pos << endl; //pos = 5 // str = " -a78"; // a = stoi(str, &pos, 8); //数字前有字母,调用会崩掉 // cout << "a = " << a << endl; // cout << "pos = " << pos << endl; return 0; }
#include <string>
long stol(const std::string& str, std::size_t* pos = 0, int base = 10);
long stol(const std::wstring& str, std::size_t* pos = 0, int base = 10);
功能:将字符串str转成 有符号 long 整数
参数:
str:字符串
pos:存储将字符串str转成有符号整数,处理了str中字符的个数的地址,默认为NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str是 0 开头为八进制,str是 0x 或 0X 开头是十六进制,默认为十进制
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { long a; size_t pos = 0; string str; str = "-1235"; a = stol(str); cout << "a = " << a << endl; //a = -1235 str = "1235"; a = stol(str); cout << "a = " << a << endl; //a = 1235 str = " -12 35"; a = stol(str, &pos); //会舍弃空白符 cout << "a = " << a << endl; //a = -12 cout << "pos = " << pos << endl; //pos = 5 str = " -12ab35"; a = stol(str, &pos); cout << "a = " << a << endl; //a = -12 cout << "pos = " << pos << endl; //pos = 5 str = "0123"; a = stol(str); cout << "a = " << a << endl; //a = 123 str = "0x123"; a = stol(str); cout << "a = " << a << endl; //a = 0 return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { long a; size_t pos = 0; string str; str = "0x123"; a = stol(str, NULL, 16); //base = 16,指定十六进制 cout << "a = " << a << endl; //a = 291 str = "0x123"; a = stol(str, NULL, 0); //base = 0,自动检测数值进制 cout << "a = " << a << endl; //a = 291 str = "-12"; a = stol(str, &pos, 16); //-(2 + 1*16) cout << "a = " << a << endl; //a = -18 cout << "pos = " << pos << endl; //pos = 3 str = "12"; a = stol(str, &pos, 16); //2 + 1*16 cout << "a = " << a << endl; //a = 18 cout << "pos = " << pos << endl; //pos = 2 str = " -12 35"; a = stol(str, &pos, 16); //会舍弃空白符 cout << "a = " << a << endl; //a = -18 cout << "pos = " << pos << endl; //pos = 5 str = " -ab"; a = stol(str, &pos, 16); //-(11 + 10*16) cout << "a = " << a << endl; //a = -171 cout << "pos = " << pos << endl; //pos = 5 str = "0123"; a = stol(str, NULL, 16); //(3 + 2*16 + 1*16*16) cout << "a = " << a << endl; //a = 291 return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { long a; size_t pos = 0; string str; str = "0x123"; a = stol(str, NULL, 8); //base = 8,指定八进制 cout << "a = " << a << endl; //a = 0 str = "0123"; //(3 + 2*8 + 1*8*8) a = stol(str, NULL, 0); //base = 0,自动检测数值进制 cout << "a = " << a << endl; //a = 83 str = "-12"; a = stol(str, &pos, 8); //-(2 + 1*8) cout << "a = " << a << endl; //a = -10 cout << "pos = " << pos << endl; //pos = 3 str = "12"; a = stol(str, &pos, 8); //2 + 1*8 cout << "a = " << a << endl; //a = 10 cout << "pos = " << pos << endl; //pos = 2 str = " -12 35"; a = stol(str, &pos, 8); //会舍弃空白符 cout << "a = " << a << endl; //a = -10 cout << "pos = " << pos << endl; //pos = 5 // str = " -a78"; // a = stol(str, &pos, 8); //数字前有字母,调用会崩掉 // cout << "a = " << a << endl; // cout << "pos = " << pos << endl; return 0; }
#include <string>
long long stoll(const std::string& str, std::size_t* pos = 0, int base = 10);
long long stoll(const std::wstring& str, std::size_t* pos = 0, int base = 10);
功能:将字符串str转成 有符号 long long 整数
参数:
str:字符串
pos:存储将字符串str转成有符号整数,处理了str中字符的个数的地址,默认为NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str是 0 开头为八进制,str是 0x 或 0X 开头是十六进制,默认为十进制
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { long long a; size_t pos = 0; string str; str = "-1235"; a = stoll(str); cout << "a = " << a << endl; //a = -1235 str = "1235"; a = stoll(str); cout << "a = " << a << endl; //a = 1235 str = " -12 35"; a = stoll(str, &pos); //会舍弃空白符 cout << "a = " << a << endl; //a = -12 cout << "pos = " << pos << endl; //pos = 5 str = " -12ab35"; a = stoll(str, &pos); cout << "a = " << a << endl; //a = -12 cout << "pos = " << pos << endl; //pos = 5 str = "0123"; a = stoll(str); cout << "a = " << a << endl; //a = 123 str = "0x123"; a = stoll(str); cout << "a = " << a << endl; //a = 0 return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { long long a; size_t pos = 0; string str; str = "0x123"; a = stoll(str, NULL, 16); //base = 16,指定十六进制 cout << "a = " << a << endl; //a = 291 str = "0x123"; a = stoll(str, NULL, 0); //base = 0,自动检测数值进制 cout << "a = " << a << endl; //a = 291 str = "-12"; a = stoll(str, &pos, 16); //-(2 + 1*16) cout << "a = " << a << endl; //a = -18 cout << "pos = " << pos << endl; //pos = 3 str = "12"; a = stoll(str, &pos, 16); //2 + 1*16 cout << "a = " << a << endl; //a = 18 cout << "pos = " << pos << endl; //pos = 2 str = " -12 35"; a = stoll(str, &pos, 16); //会舍弃空白符 cout << "a = " << a << endl; //a = -18 cout << "pos = " << pos << endl; //pos = 5 str = " -ab"; a = stoll(str, &pos, 16); //-(11 + 10*16) cout << "a = " << a << endl; //a = -171 cout << "pos = " << pos << endl; //pos = 5 str = "0123"; a = stoll(str, NULL, 16); //(3 + 2*16 + 1*16*16) cout << "a = " << a << endl; //a = 291 return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { long long a; size_t pos = 0; string str; str = "0x123"; a = stoll(str, NULL, 8); //base = 8,指定八进制 cout << "a = " << a << endl; //a = 0 str = "0123"; //(3 + 2*8 + 1*8*8) a = stoll(str, NULL, 0); //base = 0,自动检测数值进制 cout << "a = " << a << endl; //a = 83 str = "-12"; a = stoll(str, &pos, 8); //-(2 + 1*8) cout << "a = " << a << endl; //a = -10 cout << "pos = " << pos << endl; //pos = 3 str = "12"; a = stoll(str, &pos, 8); //2 + 1*8 cout << "a = " << a << endl; //a = 10 cout << "pos = " << pos << endl; //pos = 2 str = " -12 35"; a = stoll(str, &pos, 8); //会舍弃空白符 cout << "a = " << a << endl; //a = -10 cout << "pos = " << pos << endl; //pos = 5 // str = " -a78"; // a = stoll(str, &pos, 8); //数字前有字母,调用会崩掉 // cout << "a = " << a << endl; // cout << "pos = " << pos << endl; return 0; }
注意:stoi、stol、stoll 函数是C++11标准加入的,用g++编译器编译需要加参数:-std=c++11
关于“C++中的stoi/stol/stoll函数怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。