最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過(guò)stringstream實(shí)現(xiàn)常用的類(lèi)型轉(zhuǎn)換實(shí)例代碼

 更新時(shí)間:2020年04月28日 08:22:45   作者:cyssmile  
在本篇文章里小編給大家分享了關(guān)于通過(guò)stringstream實(shí)現(xiàn)常用的類(lèi)型轉(zhuǎn)換實(shí)例代碼內(nèi)容,需要的朋友們可以參考下。

其他類(lèi)型轉(zhuǎn)成string

template <class T>
void toString(string& result,const T &t)
{
  //將各種數(shù)值轉(zhuǎn)換成字符串
  ostringstream oss;
  oss.clear();
  oss << t;
  result.clear();
  result = oss.str();
}

string轉(zhuǎn)成其他類(lèi)型

template <class T>
void stringToOther(T &t, const string &s)
{
  stringstream ss;
  ss.clear();
  ss << s;
  ss >> t;
}

類(lèi)型之間的相互轉(zhuǎn)換

template <class inputType,class outputType>
void toConvert(const inputType &input, outputType &output){

  stringstream ss;
  ss.clear();
  ss << input;
  ss >> output;
}

完整代碼

#include <sstream>
#include <iostream>
#include <string>
using namespace std;

template <class T>
void toString(string& result,const T& t);
template <class T>
void stringToOther(T &t, const string &s);
template <class inputType,class outputType>
void toConvert(const inputType &input, outputType &output);

int main(int argc, char** argv)
{
  string s1;
  double a =1.1111;
  toString(s1,a);
  cout<<s1<<endl;
  double b = 0;
  double &bptr =b;
  stringToOther(bptr,s1);
  cout<<bptr<<endl;

  string s2 ="2.222";
  double c1 =0;
  double &c1ptr = c1;
  toConvert(s2,c1ptr);
  cout<<c1ptr<<endl;

  return 0;
}

template <class T>
void toString(string& result,const T &t)
{
  //將各種數(shù)值轉(zhuǎn)換成字符串
  ostringstream oss;
  oss.clear();
  oss << t;
  result.clear();
  result = oss.str();
}

template <class T>
void stringToOther(T &t, const string &s)
{
  stringstream ss;
  ss.clear();
  ss << s;
  ss >> t;
}

template <class inputType,class outputType>
void toConvert(const inputType &input, outputType &output){

  stringstream ss;
  ss.clear();
  ss << input;
  ss >> output;
}

到此這篇關(guān)于通過(guò)stringstream實(shí)現(xiàn)常用的類(lèi)型轉(zhuǎn)換實(shí)例代碼的文章就介紹到這了,更多相關(guān)stringstream實(shí)現(xiàn)常用的類(lèi)型轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

永和县| 陇川县| 庄浪县| 凤山县| 铜川市| 泰州市| 全椒县| 玉树县| 大田县| 木里| 龙泉市| 安宁市| 蒲城县| 阳高县| 松江区| 许昌县| 从化市| 屏山县| 中宁县| 乌什县| 新乡县| 兴山县| 东阳市| 九龙坡区| 石嘴山市| 云阳县| 长海县| 清水河县| 浮山县| 安多县| 武宁县| 抚顺市| 文昌市| 汉川市| 韩城市| 延寿县| 油尖旺区| 宜丰县| 门头沟区| 贵南县| 乐昌市|