详解C++中stoi/stol/stoll函数的用法

 更新时间:2023年03月23日 09:13:56   作者:微尘8  
这篇文章主要为大家详细介绍了C++中stoi、stol、stoll函数的具体用法,文中的示例代码讲解详细,对我们学校C++有一点的帮助,需要的可以参考一下

stoi()函数

#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 开头是十六进制,默认为十进制

stoi()函数指定转换字符串为十进制用法

#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;
}

stoi()函数指定转换字符串为十六进制用法

#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;
}

stoi()函数指定转换字符串为八进制用法

#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;
}

stol()函数

#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 开头是十六进制,默认为十进制

stol()函数指定转换字符串为十进制用法

#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;
}

stol()函数指定转换字符串为十六进制用法

#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;
}

stol()函数指定转换字符串为八进制用法

#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;
}

stoll()函数

#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 开头是十六进制,默认为十进制

stoll()函数指定转换字符串为十进制用法

#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;
}

stoll()函数指定转换字符串为十六进制用法

#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;
}

stoll()函数指定转换字符串为八进制用法

#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函数的用法的文章就介绍到这了,更多相关C++ stoi stol stoll内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • Windows 环境下使用 Qt 连接 MySQL

    Windows 环境下使用 Qt 连接 MySQL

    这篇文章主要介绍了Windows 环境下使用 Qt 连接 MySQL的相关资料,需要的朋友可以参考下
    2017-07-07
  • C/C++ QT实现解析JSON文件的示例代码

    C/C++ QT实现解析JSON文件的示例代码

    JSON是一种轻量级的数据交换格式,它是基于ECMAScript的一个子集,使用完全独立于编程语言的文本格式来存储和表示数据。这篇文章主要介绍了QT实现解析JSON文件的示例代码,需要的可以参考一下
    2022-01-01
  • VC++简单实现关机、重启计算机实例代码

    VC++简单实现关机、重启计算机实例代码

    这篇文章主要介绍了VC++简单实现关机、重启计算机实例代码,很实用的功能,需要的朋友可以参考下
    2014-07-07
  • C语言详细分析讲解关键字goto与void的作用

    C语言详细分析讲解关键字goto与void的作用

    我们在C语言中经常会见到void,也会偶尔见到goto,那么C语言中既然有goto,为什么我们在代码中见的很少呢?在以前很多的项目经验中,我们得到这样一条潜规则:一般项目都是禁用goto的,程序质量与goto的出现次数成反比。自后也就造成了我们一般不会使用goto
    2022-04-04
  • C++实现softmax函数的面试经验

    C++实现softmax函数的面试经验

    这篇文章主要为大家介绍了C++实现softmax函数的面试经验,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • C++中Pimpl的惯用法详解

    C++中Pimpl的惯用法详解

    Pimpl(Pointer to Implementation)是一种常见的 C++ 设计模式,用于隐藏类的实现细节,本文将通过一个较为复杂的例子,展示如何使用智能指针来实现 Pimpl 惯用法,需要的可以参考下
    2023-09-09
  • vector,map,list,queue的区别详细解析

    vector,map,list,queue的区别详细解析

    如果我们需要随机访问一个容器则vector要比list好得多。如果我们已知要存储元素的个数则vector 又是一个比list好的选择。如果我们需要的不只是在容器两端插入和删除元素则list显然要比vector好
    2013-09-09
  • 解析C++编程中virtual声明的虚函数以及单个继承

    解析C++编程中virtual声明的虚函数以及单个继承

    这篇文章主要介绍了C++编程中virtual声明的虚函数以及单个继承,剖析虚函数和单个基类所能够继承的成员,要的朋友可以参考下
    2016-01-01
  • c语言动态内存分配知识点及实例

    c语言动态内存分配知识点及实例

    在本篇文章里小编给大家整理的是关于c语言动态内存分配知识点及实例,需要的朋友们可以学习下。
    2020-03-03
  • QTimer与QTime实现电子时钟

    QTimer与QTime实现电子时钟

    这篇文章主要为大家详细介绍了QTimer与QTime实现电子时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07

最新评论