c++arraysstringc++11std

std::array<char, N> to std::string


What is the best way to convert from a std::array<char, N> to a std::string?

I have tried producing a template method but have had no luck. I think my C++ skills just aren't up to scratch. Is there an idiomatic way of doing this in C++?


Solution

  • I won't say it is the "best way", but a way is to use std::string's iterator constructor:

    std::array<char, 10> arr;
    ... // fill in arr
    std::string str(std::begin(arr), std::end(arr));