c++

How to construct a std::string from a std::vector<char>?


Short of (the obvious) building a C style string first then using that to create a std::string, is there a quicker/alternative/"better" way to initialize a string from a vector of chars?


Solution

  • Well, the best way is to use the following constructor:

    template<class InputIterator> string (InputIterator begin, InputIterator end);
    

    which would lead to something like:

    std::vector<char> v;
    std::string str(v.begin(), v.end());