Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::map<string, string> to string (first values)

I'd like to create std::string that contains first elements of std::map<std::string, std::string> separate by some delimiter (it can be from STL or Boost). Is there any better solution (one line) than loop? Like boost::algorithm::join for std::vector<std::string>.

like image 848
peter55555 Avatar asked Oct 21 '25 18:10

peter55555


1 Answers

This can be done elegantly using Boost.Range's map_keys and Boost.StringAlgo's join:

std::string get_keys(const std::map<std::string, std::string>& map) {
  return boost::algorithm::join(
    map | boost::adaptors::map_keys,
    ", ");
}

http://www.boost.org/doc/libs/1_61_0/boost/algorithm/string/join.hpp

http://www.boost.org/doc/libs/1_61_0/libs/range/doc/html/range/reference/adaptors/reference/map_keys.html

like image 127
Sebastian Redl Avatar answered Oct 23 '25 07:10

Sebastian Redl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!