返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >详解C++中stoi/stol/stoll函数的用法
  • 432
分享到

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

摘要

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

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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解C++中stoi/stol/stoll函数的用法

本文链接: https://lsjlt.com/news/200987.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • 详解C++中stoi/stol/stoll函数的用法
    目录stoi()函数stoi()函数指定转换字符串为十进制用法stoi()函数指定转换字符串为十六进制用法stoi()函数指定转换字符串为八进制用法stol()函数stol()函数指...
    99+
    2023-03-23
    C++ stoi stol stoll用法 C++ stoi stol stoll C++ stoi C++ stol C++ stoll
  • C++中的stoi/stol/stoll函数怎么使用
    这篇文章主要介绍“C++中的stoi/stol/stoll函数怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++中的stoi/stol/stoll函数怎么使用”文章能帮助大家解决问题。st...
    99+
    2023-07-05
  • C++11中的stoi&stod用法
    目录C++11的stoi & stodc++11新特性集合总结C++11的stoi & stod #include <iostream> #includ...
    99+
    2022-12-16
    C++11中stoi C++11中stod C++11 stoi & stod
  • C++中的乘法函数详解
    C++中的乘法函数详解在C++编程中,乘法是一项常见而重要的操作。C++提供了多种方式来实现乘法运算,包括基本的乘法运算符、函数重载和模板等。本文将详细介绍C++中的乘法函数的使用方法和注意事项。乘法运算符C++中的乘法运算符是*,用于对两...
    99+
    2023-11-18
    C++ 详解 乘法函数
  • C++函数指针的用法详解
    目录C++函数指针详细理解函数指针初识函数指针作为形参使用函数指针作为返回值练习总结C++函数指针详细理解 函数指针初识 函数指针是指向函数的指针,并不等于函数,它能够执行函数的操作...
    99+
    2024-04-02
  • C#中DateTime函数的详细用法
    目录一、初始化:带农历日期二、格式化函数。1、常用2、长短日期和时间三、属性1、日期部分2、时间部分3、计时周期四、日期操作1、加减几X:2、两日期差异的TimeSpan3、比较时间...
    99+
    2024-04-02
  • C语言中回调函数和qsort函数的用法详解
    目录回调函数指向函数指针数组的指针qsort(qulick sort)-库函数回调函数 通过函数指针调用的函数,如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调...
    99+
    2024-04-02
  • C/C++中extern函数使用详解
    目录一、定义和声明的区别二、extern用法2.1 extern 函数2.2 extern 变量2.3 在C++文件中调用C方式编译的函数三、通俗讲解extern一、定义和声明的区别...
    99+
    2024-04-02
  • C++中的众数函数详解
    C++中的众数函数详解在统计学中,众数指的是一组数据中出现次数最多的数值。在C++语言中,我们可以通过编写一个众数函数来找到任意一组数据中的众数。众数函数的实现可以采用多种不同的方法,下面将详细介绍其中两种常用的方法。第一种方法是使用哈希表...
    99+
    2023-11-18
    C++ 详解 众数函数
  • 详解C++中的isunordered函数
    所述isunordered()函数定义在<cmath.h>并检查是否第一个参数的值可以有意义与第二个参数进行比较。如果第一个参数不能与第二个参数进行有意义的比较(即一个或...
    99+
    2024-04-02
  • C++中的Lambda函数详解
    目录一 函数语法二 函数应用1、在普通函数中使用2、在qt信号槽中使用3、在std::sort排序函数中的使用三 总结一 函数语法 我们平时调用函数的时候,都是需要被调用函数的函数名...
    99+
    2024-04-02
  • 详解C++ functional库中的仿函数使用方法
    目录一、仿函数简介二、仿函数简要写法示例三、使用C++自带的仿函数(1)算术仿函数(2)关系仿函数(3)逻辑仿函数一、仿函数简介 仿函数(functor)又称之为函数对象(funct...
    99+
    2024-04-02
  • C语言中scanf函数的使用方法详解
    C语言中scanf函数的使用方法详解及代码示例 C语言是一门广泛应用于各种软件开发的编程语言,其中的输入输出函数在编写程序时起着非常重要的作用。其中,scanf函数是C语言中用于读取标...
    99+
    2024-02-22
    使用方法 输入输出 scanf函数
  • C++ seekg函数用法案例详解
    C++ seekg函数用法详解 很多时候用户可能会这样操作,打开一个文件,处理其中的所有数据,然后将文件倒回到开头,再次对它进行处理,但是这可能有点不同。例如,用户可能会要求程序在数...
    99+
    2024-04-02
  • C++ assert()函数用法案例详解
    1. 简介 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行。 原型定义: #include <assert.h&...
    99+
    2024-04-02
  • C++ atoi()函数用法案例详解
    目录1 功能2 格式3 注意事项3.1 关于参数的注意事项3.2 关于返回值的注意事项3.3 判断转换是否成功4 宽字符的转换1 功能 atoi()函数将数字格式的字符串转换为整数类...
    99+
    2024-04-02
  • C++sort排序函数用法详解
    目录用法两个参数用法三个参数string 使用反向迭代器来完成逆序排列最近在刷ACM经常用到排序,以前老是写冒泡,可把冒泡带到OJ里后发现经常超时,所以本想用快排,可是很多学长推荐用...
    99+
    2024-04-02
  • C/C++中时间库函数的使用详解
    目录C/C++ 中时间的概念常用的时间库函数time()asctime()gmtime()ctime()localtime()mktime()difftime()C/C++ 获取当前...
    99+
    2022-11-13
    C/C++时间库函数 C/C++时间函数 C++ 时间函数
  • C++中的随机数函数详解
    C++中的随机数函数详解随机数在计算机编程中起着重要的作用,它可以用于模拟随机事件、生成随机数序列等多种应用。C++语言中提供了一系列的随机数函数,方便开发人员进行随机数的生成和应用。本文将详细介绍C++中的随机数函数的使用方法和注意事项。...
    99+
    2023-11-18
    随机数生成器(Random Number Generator) 伪随机数(Pseudorandom Number) 随机
  • C#中的数组用法详解
    目录一.简单数组(一维数组)1.数组的声明2.数组的初始化3.访问数组元素4.数组中使用引用类型二.多维数组三.锯齿数组四.Array类1.创建数组2.复制数组3.排序五.数组作为参...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作