4

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>.

3
  • 1
    It is unclear. Say you have a map with two elements. The output you are looking for is something like "key1 : value1, key2 : value2"? Commented Sep 18, 2016 at 8:51
  • see How to retrieve all keys (or values) from a std::map?, modify the solutions to append to string Commented Sep 18, 2016 at 8:54
  • No, @Rerito . For: "key1 : value1, key2 : value2" and comma delimiter I'd like to have: "key1,key2". Commented Sep 18, 2016 at 9:50

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

Comments

5

If you don't want to use boost, then try std::accumulate:

const std::string delimiter = "#";
const std::string result = std::accumulate(M.begin(), M.end(), std::string(),
[delimiter](const std::string& s, const std::pair<const std::string, std::string>& p) {
    return s + (s.empty() ? std::string() : delimiter) + p.first;
});

In the code above M is a std::map<std::string, std::string>.

2 Comments

value_type for M is pair<const string, string>. Note the extra const
would be nice to add: #include <numeric>
2

Your algorithm should be something like this:

std::string get_keys(const std::map<std::string, std::string>& map) {
  std::string result;
  std::for_each(map.cbegin(),
                map.cend(),
                [&result](const decltype(map)::value_type& p) {
                  result += p.first;
                  result += ", ";
                });

  // Erase the last ", " from the string, if present
  if (result.size() > 0) {
    result.erase(result.size() - 2, 2);
  }

  return result;
}

Essentially you have to cycle for each element in the map and add it into the string. The complexity is O(N) with N the number of element in the map.

You can improve the performance of the algorithm applying reserve on the string result.

If you know the average length of the string key, the you can initialize the variable with:

std::string result;
result.reserve(map.size() * AVG_LENGTH_STR_KEY);

This will improve a lot the operator+= operation in the cycle.

Comments

1

As M.M. correctly pointed out, you can use boost::range for this (to which I added boost::string).

If your map is m, then the last line of

std::vector<std::string> o;
boost::copy(m | boost::adaptors::map_keys, std::back_inserter(o));
boost::algorithm::join(o, ", ");

is results. (Unfortunately, this requires a huge number of header files.)

Example

#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <boost/algorithm/string/join.hpp>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

int main()
{
    std::map<std::string, std::string> m;
    m["hello"] = "world";
    m["goodbye"] = "now";

    std::vector<std::string> o;
    boost::copy(m | boost::adaptors::map_keys, std::back_inserter(o));
    std::cout << boost::algorithm::join(o, ", ") << std::endl;
}

This outputs

$ ./a.out 
goodbye, hello
$

Comments

0

if you are using a json library such as the header only nlohmann::json

you can simply try casting it to a json in a single line. note: be careful of exceptions.

std::cout << "My map or vector: " << ((json)myMap)) << "\n";

Edit: this might lazy i suppose but it gets the job done lol.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.