当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ strtol()用法及代码示例


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

用法

strtol(s, &end, b)

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


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

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

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

以下示例程序旨在说明上述函数。

程序1

// C++ program to illustrate the 
// strtol() function when decimal base 
#include <cstdlib> 
#include <cstring> 
#include <iostream> 
#include <string> 
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 <cstdlib> 
#include <cstring> 
#include <iostream> 
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 <cstdlib> 
#include <iostream> 
  
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 <cstdlib> 
#include <iostream> 
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


相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 strtol() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。