C++字符串数值的转换(to_string/stoi/stod)

  1. <code class="language-cpp" deep="7">
  2. #include<iostream>  
  3. #include<string>  
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     string s1 = "2018.11";  
  10.     int a1 = stoi(s1);  
  11.     double c = stod(s1);  
  12.     float d = stof(s1);  
  13.     int a2 = a1 + 1;  
  14.     string b = to_string(a1);  
  15.     b.append(" is a string");  
  16.       
  17.     cout << a1 <<"/"<<c<<"/"<<d<<"/"<<a2<<"/"<<b<< endl;  
  18. }</code>  


猜你喜欢

转载自blog.csdn.net/u010112268/article/details/81258811