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>.
-
1It is unclear. Say you have a map with two elements. The output you are looking for is something like "key1 : value1, key2 : value2"?Rerito– Rerito2016-09-18 08:51:23 +00:00Commented Sep 18, 2016 at 8:51
-
see How to retrieve all keys (or values) from a std::map?, modify the solutions to append to stringM.M– M.M2016-09-18 08:54:59 +00:00Commented Sep 18, 2016 at 8:54
-
No, @Rerito . For: "key1 : value1, key2 : value2" and comma delimiter I'd like to have: "key1,key2".peter55555– peter555552016-09-18 09:50:48 +00:00Commented Sep 18, 2016 at 9:50
5 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
Comments
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 constYour 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
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
$