Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert vector<double> to vector<string> ( elegant way )

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;
}
like image 979
CroCo Avatar asked Aug 18 '14 20:08

CroCo


People also ask

How do you turn a vector into a string?

Convert Vector to String using toString() function To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.

How do I remove a string from a vector?

To remove all copies of an element from a vector , you can use std::remove like this: v. erase(std::remove(v. begin(), v.

How do you compare vector strings?

Comparing two vectors using operator == std::vector provides an equality comparison operator==, it can be used to compare the contents of two vectors. For each element in the vector it will call operator == on the elements for comparisons.


1 Answers

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

like image 61
quantdev Avatar answered Sep 21 '22 14:09

quantdev