Convert vector to vector ( elegant way )

后端 未结 3 2129
旧巷少年郎
旧巷少年郎 2021-02-13 14:46

I would like to know if there is an elegant way or a built-in function to convert vector to vector. What I\'ve done is simp

相关标签:
3条回答
  • 2021-02-13 15:08

    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

    0 讨论(0)
  • 2021-02-13 15:11

    In general, if you have a container of T and want to create a container of U from the container of T, as others have mentioned the algorithm to look for is std::transform.

    If you are not using C++ 11, Here is std::transform usage:

    #include <algorithm>
    #include <vector>
    #include <string>
    #include <iostream>
    #include <iterator>
    #include <sstream>
    
    std::string Transformer(double d)
    {
        std::ostringstream doubleStr;
        doubleStr << d;
        return doubleStr.str();
    }
    
    int main()
    {
        std::vector<double> doubleVec;
        doubleVec.push_back(1.0);
        doubleVec.push_back(2.1);
        doubleVec.push_back(3.2);
    
        std::vector<std::string> doubleStr;
        std::transform(doubleVec.begin(), doubleVec.end(), std::back_inserter(doubleStr), Transformer);
        std::copy(doubleStr.begin(), doubleStr.end(), std::ostream_iterator<std::string>(std::cout, "  "));
    }
    

    Output: 1 2.1 3.2

    0 讨论(0)
  • 2021-02-13 15:15

    Using copy and ostream_iterator:

    #include <vector>
    #include <iostream>
    #include <sstream>
    #include <iterator>
    
    int main()
    {
      std::vector<double> numbers{1.0, 2.1, 3.2};
      std::stringstream output;
      std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<double>(output, " "));
    
      std::cout << output.str() << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题