當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。