將數值轉換為字符串
用法:
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); 參數: val - Numerical value. 返回值: A string object containing the representation of val as a sequence of characters.
// CPP program to illustrate
// std::to_string
#include <bits/stdc++.h>
// 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 <bits/stdc++.h>
// 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
相關用法
注:本文由純淨天空篩選整理自 std::to_string in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。