详解 C++ STL 中的 strtol() 函数

今天,让我们一起来探索 C++ STL 中一个非常实用的内置函数——strtol()。这个函数的主要功能是将字符串的内容转换为指定进制的整数,并将其作为 long int 类型返回。语法如下:

strtol(s, &end, b)

参数说明

我们需要向该函数传递三个必要的参数,具体描述如下:

  • s:指定我们要进行转换的字符串,该字符串包含了一个整数的表示形式。
  • end:这是一个指向 INLINECODE834e610e 类型的指针,用于指示转换停止的位置。函数会将 INLINECODE95a2a117 的值设置为字符串 s 中最后一个有效字符之后的下一个字符。它也可以是一个空指针(Null pointer),在这种情况下,该参数将不会被使用。
  • b:指定整数的基数(即进制)。

返回值

该函数的返回值主要分为两种情况:

  • 如果发生了有效的转换,函数将返回转换后的 long int 值。
  • 如果没有发生有效的转换,函数将返回 0。

为了帮助大家更好地理解,下面我们通过几个程序来演示上述函数的用法。

程序 1

在这个例子中,我们将展示如何使用 strtol() 函数处理十进制字符串。

// C++ program to illustrate the
// strtol() function when decimal base
#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    int b = 10;
    char s[] = "6010IG_2016p";
    char* end;
    long int n;

    n = strtol(s, &end, b);
    cout << "Number in  String = " << s << endl;
    cout << "Number in Long Int = " << n << endl;
    cout << "End String = " << end << endl
         << endl;

    // the pointer to invalid
    // characters can be null
    strcpy(s, "47");
    cout << "Number in  String = " << s << endl;
    n = strtol(s, &end, b);
    cout << "Number in Long Int = " << n << endl;
    if (*end) {
        cout << end;
    }
    else {
        cout << "Null pointer";
    }
    return 0;
}

输出结果:

Number in  String = 6010IG_2016p
Number in Long Int = 6010
End String = IG_2016p

Number in  String = 47
Number in Long Int = 47
Null pointer

程序 2

接下来,让我们看看在不同进制下的转换效果。

// C++ program to illustrate the
// strtol() function
#include 
#include 
#include 
using namespace std;

int main()
{
    char* end;

    cout << "489bc"
         << " to Long Int with base-4 = "
         << strtol("489bc", &end, 4) << endl;
    cout << "End String = " << end << endl;

    cout << "123s"
         << " to Long Int with base-11 = "
         << strtol("123s", &end, 11) << endl;
    cout << "End String = " << end << endl;

    cout << "56xyz"
         << " to Long Int with base-36 = "
         << strtol("56xyz", &end, 36) << endl;
}

输出结果:

489bc to Long Int with base-4 = 0
End String = 489bc
123s to Long Int with base-11 = 146
End String = s
56xyz to Long Int with base-36 = 8722043

程序 3

当我们将基数(base)设置为 0 时,函数会自动判断字符串的进制(八进制、十六进制或十进制)。让我们看看下面的例子:

// C++ program to illustrate the
// strtol() function when base is 0
#include 
#include 

using namespace std;

int main()
{
    char* end;

    // octal base
    cout << "312gfg"
         << " to Long Int with base-0 = "
         << strtol("312gfg", &end, 0) << endl;
    cout << "End String = " << end << endl
         << endl;

    // hexadecimal base
    cout << "0q15axtz"
         << " to Long Int with base-0 = "
         << strtol("0q15axtz", &end, 0) << endl;
    cout << "End String = " << end << endl
         << endl;

    // decimal base
    cout << "33ffn"
         << " to Long Int with base-0 = "
         << strtol("33ffn", &end, 0) << endl;
    cout << "End String = ";

    return 0;
}

输出结果:

312gfg to Long Int with base-0 = 312
End String = gfg

0q15axtz to Long Int with base-0 = 0
End String = q15axtz

33ffn to Long Int with base-0 = 33
End String =

程序 4

最后,我们来处理一些特殊情况,比如无效的转换和字符串开头的空白字符。

// C++ program to illustrate the
// strtol() function for invalid
// conversions and leading whitespaces.
#include 
#include 
using namespace std;

int main()
{
    char* end;

    cout << "22abcd"
         << " to Long Int with base-6 = "
         << strtol("  22abcd", &end, 6) << endl;
    cout << "End String = " << end << endl
         << endl;

    cout << "114cd"
         << " to Long Int with base-2 = "
         << strtol("   114cd", &end, 2) << endl;
    cout << "End String = " << end << endl
         << endl;

    cout << "e10.79"
         << " to Long Int with base-10 = "
         << strtol("e10.79", &end, 10) << endl;

    cout << "End String = " << end << endl
         << endl;

    return 0;
}

输出结果:

22abcd to Long Int with base-6 = 14
End String = abcd

114cd to Long Int with base-2 = 3
End String = 4cd

e10.79 to Long Int with base-10 = 0
End String = e10.79
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/53024.html
点赞
0.00 平均评分 (0% 分数) - 0