zoukankan      html  css  js  c++  java
  • 【面试题目】-string与int/float间的转换

    // changstring.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <cstdlib>
    using namespace std;

    template <class T>
    void convertFromString(T &, const std::string &);

    template <class T>
    std::string ConvertToString(T);

    int _tmain(int argc, _TCHAR* argv[])
    {
       std::string s("123");

      // Convert std::string to int
      int i = 0;
      convertFromString(i,s);
      std::cout << i << std::endl;
      float f=12.5;
      string str=ConvertToString(f);
      cout<<str<<endl;
      cin>>i;
    }

    template <class T>
    void convertFromString(T &value, const std::string &s)
    {
       std::stringstream ss(s);
       ss >> value;
    }

    template <class T>
    std::string ConvertToString(T value)
    {
        std::stringstream ss;
        ss<<value;
        return ss.str();
    }

  • 相关阅读:
    坐火车/长途汽车去
    图书管理系统设计
    电梯演说模板练习
    敏捷开发
    团队形式
    android app demo
    classic code review
    code review
    阅读思考
    单元测试
  • 原文地址:https://www.cnblogs.com/dracohan/p/1362616.html
Copyright © 2011-2022 走看看