stoul() 函数

#include <string>

unsigned long stoul(const std::string& str, std::size_t* pos = 0, int base = 10);

unsigned long stoul(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:将字符串 str 转成 unsigned long 整数
参数:
str:字符串
pos:存储将字符串 str 转成 unsigned long 整数,处理了 str 中字符的个数的地址,默认为 NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str 是 0 开头为八进制,str 是 0x 或 0X 开头是十六进制,默认为十进制

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

stoul.cpp

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;int main(int argc, char *argv[])
{unsigned long a; //x86_64构架下,unsigned long 8个字节size_t pos = 0;string str;cout << "ULONG_MAX = " << ULONG_MAX << endl;str = "-1"; a = stoul(str); //数据在内存中是以补码的形式存储的,负数的补码等于反码加1/*-1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001-1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110-1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111*/cout << "a = " << a << endl; // a = (最大值) +(负数)+ 1,即:((2^64) - 1) + (-1) + 1 str = "1235";a = stoul(str);cout << "a = " << a << endl; //a = 1235str = "  -12  35"; a = stoul(str, &pos); //会舍弃空白符cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1cout << "pos = " << pos << endl; //pos = 5str = "  -12ab35";a = stoul(str, &pos);cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1cout << "pos = " << pos << endl; //pos = 5str = "0123";a = stoul(str);cout << "a = " << a << endl; //a = 123str = "0x123";a = stoul(str);cout << "a = " << a << endl; //a = 0return 0;
}

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

stoul.cpp

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;int main(int argc, char *argv[])
{unsigned long a; //x86_64构架下,unsigned long 8个字节size_t pos = 0;string str;cout << "ULONG_MAX = " << ULONG_MAX << endl;/*数据在内存中是以补码的形式存储的,负数的补码等于反码加1-1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001-1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110-1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111-1转成unsigned long:(最大值) +(负数)+ 1-1转成unsigned long:((2^64) - 1) + (-1) + 1*/str = "0x123"; a = stoul(str, NULL, 16); //base = 16,指定十六进制cout << "a = " << a << endl; //a = 3 + 2*16 + 1*16*16 = 291str = "0x123";a = stoul(str, NULL, 0); //base = 0,自动检测数值进制cout << "a = " << a << endl; //a = 291str = "  -12"; a = stoul(str, &pos, 16); //会舍弃空白符cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1cout << "pos = " << pos << endl; //pos = 5str = "  -12 35";a = stoul(str, &pos, 16);cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1cout << "pos = " << pos << endl; //pos = 5str = "  -ab";a = stoul(str, NULL, 16);cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(11 + 10*16)) + 1str = "0123";a = stoul(str, NULL, 16);   cout << "a = " << a << endl; //a = (3 + 2*16 + 1*16*16) = 291return 0;
}

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

stoul.cpp

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;int main(int argc, char *argv[])
{unsigned long a; //x86_64构架下,unsigned long 8个字节size_t pos = 0;string str;cout << "ULONG_MAX = " << ULONG_MAX << endl;/*数据在内存中是以补码的形式存储的,负数的补码等于反码加1-1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001-1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110-1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111-1转成unsigned long:(最大值) +(负数)+ 1-1转成unsigned long:((2^64) - 1) + (-1) + 1*/str = "0x123";a = stoul(str, NULL, 8); //base = 8,指定八进制cout << "a = " << a << endl; //a = 0str = "0123"; //(3 + 2*8 + 1*8*8)a = stoul(str, NULL, 0); //base = 0,自动检测数值进制cout << "a = " << a << endl; //a = (3 + 2*8 + 1*8*8) = 83str = "-12";a = stoul(str, &pos, 8); //-(2 + 1*8)cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1cout << "pos = " << pos << endl; //pos = 3str = "12";a = stoul(str, &pos, 8); //2 + 1*8cout << "a = " << a << endl; //a = 10cout << "pos = " << pos << endl; //pos = 2str = "  -12  35"; a = stoul(str, &pos, 8); //会舍弃空白符cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1cout << "pos = " << pos << endl; //pos = 5// str = "  -a78"; // a = stol(str, &pos, 8); //数字前有字母,调用会崩掉// cout << "a = " << a << endl; // cout << "pos = " << pos << endl; return 0;
}

stoull() 函数

#include <string>

unsigned long long stoull(const std::string& str, std::size_t* pos = 0, int base = 10);

unsigned long long stoull(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:将字符串 st r转成 unsigned long long 整数
参数:
str:字符串
pos:存储将字符串 str 转成 unsigned long long 整数,处理了 str 中字符的个数的地址,默认为 NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str 是 0 开头为八进制,str 是 0x 或 0X 开头是十六进制,默认为十进制

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

stoull.cpp

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;int main(int argc, char *argv[])
{unsigned long long a; //x86_64构架下,unsigned long long 8个字节size_t pos = 0;string str;cout << "ULLONG_MAX = " << ULLONG_MAX << endl;str = "-1"; a = stoull(str); //数据在内存中是以补码的形式存储的,负数的补码等于反码加1/*-1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001-1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110-1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111*/cout << "a = " << a << endl; // a = (最大值) +(负数)+ 1,即:((2^64) - 1) + (-1) + 1 str = "1235";a = stoull(str);cout << "a = " << a << endl; //a = 1235str = "  -12  35"; a = stoull(str, &pos); //会舍弃空白符cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1cout << "pos = " << pos << endl; //pos = 5str = "  -12ab35";a = stoull(str, &pos);cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1cout << "pos = " << pos << endl; //pos = 5str = "0123";a = stoull(str);cout << "a = " << a << endl; //a = 123str = "0x123";a = stoull(str);cout << "a = " << a << endl; //a = 0return 0;
}

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

stoull.cpp

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;int main(int argc, char *argv[])
{unsigned long long a; //x86_64构架下,unsigned long long 8个字节size_t pos = 0;string str;cout << "ULLONG_MAX = " << ULLONG_MAX << endl;/*数据在内存中是以补码的形式存储的,负数的补码等于反码加1-1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001-1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110-1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111-1转成unsigned long long:(最大值) +(负数)+ 1-1转成unsigned long long:((2^64) - 1) + (-1) + 1*/str = "0x123"; a = stoull(str, NULL, 16); //base = 16,指定十六进制cout << "a = " << a << endl; //a = 3 + 2*16 + 1*16*16 = 291str = "0x123";a = stoull(str, NULL, 0); //base = 0,自动检测数值进制cout << "a = " << a << endl; //a = 291str = "  -12"; a = stoull(str, &pos, 16); //会舍弃空白符cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1cout << "pos = " << pos << endl; //pos = 5str = "  -12 35";a = stoull(str, &pos, 16);cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1cout << "pos = " << pos << endl; //pos = 5str = "  -ab";a = stoull(str, NULL, 16);cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(11 + 10*16)) + 1str = "0123";a = stoull(str, NULL, 16);   cout << "a = " << a << endl; //a = (3 + 2*16 + 1*16*16) = 291return 0;
}

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

stoul.cpp

#include <iostream>
#include <string>
#include <limits.h>
using namespace std;int main(int argc, char *argv[])
{unsigned long long a; //x86_64构架下,unsigned long long 8个字节size_t pos = 0;string str;cout << "ULLONG_MAX = " << ULLONG_MAX << endl;/*数据在内存中是以补码的形式存储的,负数的补码等于反码加1-1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001-1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110-1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111-1转成unsigned long long:(最大值) +(负数)+ 1-1转成unsigned long long:((2^64) - 1) + (-1) + 1*/str = "0x123";a = stoul(str, NULL, 8); //base = 8,指定八进制cout << "a = " << a << endl; //a = 0str = "0123"; //(3 + 2*8 + 1*8*8)a = stoul(str, NULL, 0); //base = 0,自动检测数值进制cout << "a = " << a << endl; //a = (3 + 2*8 + 1*8*8) = 83str = "-12";a = stoul(str, &pos, 8); //-(2 + 1*8)cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1cout << "pos = " << pos << endl; //pos = 3str = "12";a = stoul(str, &pos, 8); //2 + 1*8cout << "a = " << a << endl; //a = 10cout << "pos = " << pos << endl; //pos = 2str = "  -12  35"; a = stoul(str, &pos, 8); //会舍弃空白符cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1cout << "pos = " << pos << endl; //pos = 5// str = "  -a78"; // a = stol(str, &pos, 8); //数字前有字母,调用会崩掉// cout << "a = " << a << endl; // cout << "pos = " << pos << endl; return 0;
}

总结:

stoul 函数:将字符串转成 unsigned long 整数。

stoull 函数:将字符串转成 unsigned long long 整数。

使用时需要注意的是 stoul、stoull 函数是 C++11标准加入的,用 g++ 编译器编译时需要加参数:-std=c++11

C++stoul、stoull 函数用法相关推荐

  1. C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull

    文章目录 1 std::stod 2 std::stof 3 std::stoi 4 std::stol 5 std::stold 6 std::stoll 7 std::stoul 8 std::s ...

  2. 2021年大数据常用语言Scala(三十七):scala高级用法 高阶函数用法

    目录 高阶函数用法 作为值的函数 匿名函数 柯里化(多参数列表) 闭包 高阶函数用法 Scala 混合了面向对象和函数式的特性,在函数式编程语言中,函数是"头等公民",它和Int. ...

  3. php foreach嵌套foreach,php中foreach怎么嵌套foreach PHP中foreach函数用法?

    foreach的使用方法小编不是很明确,分享达人指教一下.foreach (array_expressforeach($array as $key) { if(xxxx) { break; //bre ...

  4. linux中awk下 gsub函数用法

    gsub函数则使得在所有正则表达式被匹配的时候都发生替换 gsub(regular expression, subsitution string, target string);简称 gsub(r,s ...

  5. ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多)

    ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多) https://blog.csdn.net/qq_25221835/article/details/82762416 post ...

  6. 详细记录python的range()函数用法

    详细记录python的range()函数用法 使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的sl ...

  7. python Pool常用函数用法总结

    在本篇内容里小编给大家整理的是一篇关于python Pool常用函数用法总结内容,有需要的朋友们可以学习下. 1.说明 apply_async(func[,args[,kwds]):使用非堵塞调用fu ...

  8. C++计算程序耗时函数用法汇总

    文章目录 前言 方法一(使用频率:常用) 方法二(使用频率:次之) 方法三(使用频率:常用) 补充:方法四 方法五 最常用的时间戳获取方法 前言 这里简单将自己在项目中经常用到的统计耗时的函数用法做个 ...

  9. undistortPoints()函数用法总结

    undistortPoints()函数用法总结   函数调用:C++: void undistortPoints(InputArray src, OutputArray dst, InputArray ...

最新文章

  1. 通过启动项设置实现应用程序自启动功能
  2. 18、Power Query-SQL筛选
  3. PP视频怎么把输出声音设置成单声道输出
  4. 分布式存储--理解分布式文件系统、分布式块存储、分布式对象存储、分布式数据库
  5. 禁用计算机端口,电脑如何关闭445端口
  6. 在IDEA中如何使用eclipse快捷键
  7. QAV250四轴穿越机安装全程详解(多图)
  8. Uniapp Android原生插件开发
  9. 计算机忽然打开东西特别慢,电脑突然打开网页很慢
  10. spdep | 如何在R语言中计算空间自相关指数
  11. 1041 考试座位号 (15 分)
  12. 离线地图-geoserver
  13. [vijos P1391] 想越狱的小杉
  14. 深夜有感而发的第一个文章
  15. Matlab plot画图 坐标字体、字号、范围、间隔等的设置
  16. MeterSphere一站式开源持续测试平台
  17. Spark教程——(10)Spark SQL读取Phoenix数据本地执行计算
  18. 学习MySQL的第一步:安装MySQL及数据库可视化工具Navicat
  19. 通过ip获取所在地理位置、通过手机号获取归属地
  20. asp.net 页面加载

热门文章

  1. 测试人生 | 疫情之下工资翻了2倍多,这4个月学习比工作8年学到的还多
  2. ios APP性能检测
  3. 【分享笔记】android6.0特性
  4. print函数的高级用法(输出到文件,自定义间隔符,强制刷新)
  5. AI基础:卷积神经网络
  6. 怎么保护地球生物多样性
  7. 国开电大 光伏电池原理与工艺 形考任务
  8. 小程序授权第三方平台
  9. QT笔记——Q_Q 和Q_D 学习
  10. 航信软件里面的虚拟服务器,航天信息网络应用平台