C++中将数字转换为字符串

梦里梦外; 2023-10-02 17:52 99阅读 0赞
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. using namespace std;
  5. template<typename T>
  6. void to_string(T value) {
  7. std::ostringstream os;
  8. os << value;
  9. return os.str;
  10. }
  11. int main() {
  12. int lcc1 = 1;
  13. long lcc2 = 122;
  14. double lcc3 = 2.1;
  15. string s1 = to_string(lcc1);
  16. string s2 = to_string(lcc2);
  17. string s3 = to_string(lcc3);
  18. cout << s1 << "\n" << s2 << "\n" << s3 << endl;
  19. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读

    相关 字符串转换数字

    声明:题目来源于《王道》 问题描述:输入一个表示整数的字符串,把该字符串转换成整数并输出。例如,输入字符串“12345”,输出整数“12345”。 解决方法:依次扫描字符串