当前位置:网站首页>Conversion between numeric types and strings

Conversion between numeric types and strings

2022-06-22 16:16:00 Ordinary people who like playing basketball

1. Convert numeric values to strings

Use to_string() Method can easily convert various numeric types to string types , This is an overloaded function , Function declaration in header file in ,

  • The function prototype is as follows :
//  The header file  <string>
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
  • The use of functions is very simple , The sample code is as follows :
#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    string pi = "pi is " + to_string(3.1415926);
    string love = "love is " + to_string(5.20 + 13.14);
    cout << pi << endl;
    cout << love << endl;
    return 0;
}
  • test :
     Insert picture description here

2. Convert a string to a numeric value

because C++ Numeric types in include integer and floating point types , Therefore, different functions are provided for different types , By calling these functions, you can convert the string type to the corresponding numeric type .

//  Defined in header file  <string>
int       stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
long      stol( const std::string& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );

unsigned long      stoul( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long long stoull( const std::string& str, std::size_t* pos = 0, int base = 10 );

float       stof( const std::string& str, std::size_t* pos = 0 );
double      stod( const std::string& str, std::size_t* pos = 0 );
long double stold( const std::string& str, std::size_t* pos = 0 );
  • str: String to convert
  • pos: Out parameter , The record cannot continue to be parsed from which character , such as : 123abc, The outgoing location is 3
  • base: if base by 0 , Then the value is automatically detected : If the prefix is 0 , It's octal , If the prefix is 0x or 0X, Is hexadecimal , Otherwise it is decimal .

Although these functions have multiple parameters , But except for the first parameter, everything else has a default value , Generally, using default values can meet the requirements .

  • eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
    
    string str1 = "45";
    string str2 = "3.14159";
    string str3 = "9527 with words";
    string str4 = "words and 2";

    int myint1 = std::stoi(str1);
    float myint2 = std::stof(str2);
    int myint3 = std::stoi(str3);
    //  error : 'std::invalid_argument'
    // int myint4 = std::stoi(str4);

    cout << "std::stoi(\"" << str1 << "\") is " << myint1 << endl;
    cout << "std::stof(\"" << str2 << "\") is " << myint2 << endl;
    cout << "std::stoi(\"" << str3 << "\") is " << myint3 << endl;
    // cout << "std::stoi(\"" << str4 << "\") is " << myint4 << endl;
}

  • test :
     Insert picture description here
    From the above test procedure, it can be concluded that , stay C++11 These conversion functions are provided to convert strings to numeric values :

  • If all characters in the string are numeric , The entire string will be converted to the corresponding numeric value , And return... Through the return value

  • If the first half of the string is of numeric type , The second half is not , Then the first half will be converted to the corresponding value , And return... Through the return value

  • If the first character of a character is not a numeric value, the conversion fails

  • Reference resources : Conversion between numeric type and string

原网站

版权声明
本文为[Ordinary people who like playing basketball]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221446592997.html