C++stoul、stoull 函数用法

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 = 1235

    str = "  -12  35"; 
    a = stoul(str, &pos); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12ab35";
    a = stoul(str, &pos);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "0123";
    a = stoul(str);
    cout << "a = " << a << endl; //a = 123

    str = "0x123";
    a = stoul(str);
    cout << "a = " << a << endl; //a = 0

    return 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 = 291

    str = "0x123";
    a = stoul(str, NULL, 0); //base = 0,自动检测数值进制
    cout << "a = " << a << endl; //a = 291

    str = "  -12"; 
    a = stoul(str, &pos, 16); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12 35";
    a = stoul(str, &pos, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -ab";
    a = stoul(str, NULL, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(11 + 10*16)) + 1

    str = "0123";
    a = stoul(str, NULL, 16);   
    cout << "a = " << a << endl; //a = (3 + 2*16 + 1*16*16) = 291

    return 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 = 0

   str = "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) = 83

    str = "-12";
    a = stoul(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "pos = " << pos << endl; //pos = 3

    str = "12";
    a = stoul(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2

    str = "  -12  35"; 
    a = stoul(str, &pos, 8); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "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 = 1235

    str = "  -12  35"; 
    a = stoull(str, &pos); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12ab35";
    a = stoull(str, &pos);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "0123";
    a = stoull(str);
    cout << "a = " << a << endl; //a = 123

    str = "0x123";
    a = stoull(str);
    cout << "a = " << a << endl; //a = 0

    return 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 = 291

    str = "0x123";
    a = stoull(str, NULL, 0); //base = 0,自动检测数值进制
    cout << "a = " << a << endl; //a = 291

    str = "  -12"; 
    a = stoull(str, &pos, 16); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12 35";
    a = stoull(str, &pos, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -ab";
    a = stoull(str, NULL, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(11 + 10*16)) + 1

    str = "0123";
    a = stoull(str, NULL, 16);   
    cout << "a = " << a << endl; //a = (3 + 2*16 + 1*16*16) = 291

    return 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 = 0

   str = "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) = 83

    str = "-12";
    a = stoul(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "pos = " << pos << endl; //pos = 3

    str = "12";
    a = stoul(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2

    str = "  -12  35"; 
    a = stoul(str, &pos, 8); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值