📜  C++中的std :: to_string

📅  最后修改于: 2021-05-30 16:36:04             🧑  作者: Mango

将数值转换为字符串

句法 :

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);

Parameters :
val - Numerical value.

Return Value :
A string object containing the representation of val as a sequence of characters.
// CPP program to illustrate
// std::to_string
#include 
  
// Driver code
int main()
{
  
    // Converting float to string
    std::string str1 = std::to_string(12.10);
  
    // Converting integer to string
    std::string str2 = std::to_string(9999);
  
    // Printing the strings
    std::cout << str1 << '\n';
    std::cout << str2 << '\n';
    return 0;
}

输出:

12.100000
9999

问题:查找给定整数中的特定数字。
例子 :

Input : number  = 10340, digit = 3
Output : 3 is at position 3
// CPP code to find a digit in a number
// using std::tostring
#include 
  
// Driver code
int main()
{
  
    // Converting number to string
    std::string str = std::to_string(9954);
  
    // Finding 5 in the number
    std::cout << "5 is at position " << str.find('5') + 1;
}

输出 :

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