📜  C++ STL中的strtol()函数

📅  最后修改于: 2021-05-30 05:12:38             🧑  作者: Mango

strtol()函数是C++ STL中的内置函数,该函数将字符串的内容转换为指定基数的整数,并以long int形式返回其值。

语法

strtol(s, &end, b)

参数:该函数接受三个强制性参数,如下所述:

  • s :指定具有整数表示形式的字符串。
  • end :指的是已经分配的char *类型的对象。 end的值由函数设置为s中最后一个有效字符之后的s中的下一个字符。它也可以是空指针,在这种情况下不使用它。
  • b :指定整数值的底数。

返回值:函数返回两种类型的值:

  • 如果发生有效转换,则返回long int值。
  • 如果没有有效的转换发生,则返回0。

下面的程序说明了上述函数。

程序1

// 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

// 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”