转换向量< double>到向量< string> (优雅的方式) [英] Convert vector<double> to vector<string> ( elegant way )

查看:182
本文介绍了转换向量< double>到向量< string> (优雅的方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个优雅的方式或内置函数将向量< double> 转换为向量< string& 。我所做的是简单的

  #include< iostream& 
#include< string>
#include< vector>
#include< sstream>


std :: vector< std :: string> doubeVecToStr(const std :: vector< double>& vec)
{
std :: vector< std :: string> tempStr

for(unsigned int i(0); i< vec.size(); ++ i){
std :: ostringstream doubleStr;
doubleStr<< vec [i];
tempStr.push_back(doubleStr.str());
}

return tempStr;
}


int main(int argc,char * argv [])
{
std :: vector< double> doubleVec;
doubleVec.push_back(1.0);
doubleVec.push_back(2.1);
doubleVec.push_back(3.2);

std :: vector< std :: string> doubleStr;
doubleStr = doubeVecToStr(doubleVec);

for(unsigned int i(0); i< doubleStr.size(); ++ i)
std :: cout< doubleStr [i]< ;

std :: cout<< std :: endl;

return 0;
}


解决方案

标准解决方案是使用 std :: transform 使用 std :: to_string 进行转换:

  std :: transform(std :: begin(doubleVec),
std :: end(doubleVec),
std :: back_inserter(doubleStr),
[](double d){return std :: to_string(d);}
);

您可以将其包装在函数模板中,使其可以与任何符合标准的容器一起使用:

  template< class IteratorIn,class IteratorOut> 
void to_string(IteratorIn first,IteratorIn last,IteratorOut out)
{
std :: transform(first,last,out,
[](typename std :: iterator_traits< IteratorIn> ; :: value_type d){return std :: to_string(d);});
}

或在 C ++ 14 泛型lambda:

 模板< class IteratorIn,class IteratorOut> 
void to_string(IteratorIn first,IteratorIn last,IteratorOut out)
{
std :: transform(first,last,out,[](auto d){return std :: to_string );});
}

使用任何容器调用它(即它与 std :: list< int> ,例如):

  to_string (doubleVec),std :: end(doubleVec),std :: back_inserter(doubleStr)); 






注意: / p>


  • 如果您没有C ++ 11编译器,请编写您自己的 to_string 功能模板:



示例:

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

并以类似的方式使用:

  std :: transform(doubleVec.begin(),
doubleVec.end(),
std :: back_inserter(doubleStr),
my_to_string< double>);




  • 您应该 reserve() 输出向量中的内存以避免 std :: transform()



这样做:

  std :: vector< std :: string> stringVec; 
stringVec.reserve(v.size()); //为v.size()元素保留空间






在线演示


I would like to know if there is an elegant way or a built-in function to convert vector<double> to vector<string>. What I've done is simple

#include <iostream>
#include <string>
#include <vector>
#include <sstream>


std::vector<std::string> doubeVecToStr(const std::vector<double>& vec)
{
    std::vector<std::string> tempStr;

    for (unsigned int i(0); i < vec.size(); ++i){
        std::ostringstream doubleStr;
        doubleStr << vec[i];    
        tempStr.push_back(doubleStr.str());
    }

    return tempStr;
}


int main( int argc, char* argv[] )
{
    std::vector<double> doubleVec;
    doubleVec.push_back(1.0);
    doubleVec.push_back(2.1);
    doubleVec.push_back(3.2);

    std::vector<std::string> doubleStr;
    doubleStr = doubeVecToStr(doubleVec);

    for (unsigned int i(0); i < doubleStr.size(); ++i)
        std::cout << doubleStr[i] << "  ";

    std::cout << std::endl;

    return 0;
}

解决方案

There are many ways, but a standard solution is to use std::transform with a lambda using std::to_string for the conversion :

std::transform(std::begin(doubleVec),
               std::end(doubleVec), 
               std::back_inserter(doubleStr),
               [](double d) { return std::to_string(d); } 
              );

And you can wrap that in a function template to make it work with any Standard compliant container :

template<class IteratorIn, class IteratorOut>
void to_string(IteratorIn first, IteratorIn last, IteratorOut out)
{
    std::transform(first, last, out,
                   [](typename std::iterator_traits<IteratorIn>::value_type d) { return std::to_string(d); } );
}

Or in C++14, with a generic lambda :

template<class IteratorIn, class IteratorOut>
void to_string(IteratorIn first, IteratorIn last, IteratorOut out)
{
    std::transform(first, last, out, [](auto d) { return std::to_string(d); } );
}

And call it with any container (i.e. it works with std::list<int>, for instance) :

to_string(std::begin(doubleVec), std::end(doubleVec), std::back_inserter(doubleStr));


Notes :

  • If you don't have a C++11 compiler, write your own to_string function template :

Example:

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

And use it in a similar way :

std::transform(doubleVec.begin(),
               doubleVec.end(),
               std::back_inserter(doubleStr), 
               my_to_string<double> );

  • You should reserve() the memory in the output vector to avoid reallocations during std::transform() :

e.g. do this :

std::vector<std::string> stringVec;
stringVec.reserve(v.size());   // reserve space for v.size() elements


Live demo

这篇关于转换向量&lt; double&gt;到向量&lt; string&gt; (优雅的方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆