1

As far as I looked through the C++ reference, there is no function which returns a vector (or similar) containing all the keywords of a map. Why is this so? This is not a super important function, but sometimes it can be useful. (I don't exactly understand the key_comp function, but this seems to be not what I'm looking for)

Just for completeness, here is some function which does what I want:

template<typename key_t, typename value_t>
std::vector<key_t> getKeywords(const std::map<key_t, value_t>& m){
  std::vector<key_t> keywords;
  for(const auto& it: m){
    keywords.push_back(it.first);
  }
  return keywords;
}
marco
  • 243
  • 1
  • 9
  • Possible duplicate of [How to retrieve all keys (or values) from a std::map and put them into a vector?](https://stackoverflow.com/questions/110157/how-to-retrieve-all-keys-or-values-from-a-stdmap-and-put-them-into-a-vector) – QCTDev Aug 06 '19 at 13:58
  • (Note that QCTDev links to an old question. You want the "C++0x" answer now - C++0x became C++11, and all modern compilers support it) – MSalters Aug 06 '19 at 14:01
  • note: the `key_comp` function is used to maintain an ordering in the map. If you don't need that, look at `unordered_map` – xtofl Aug 06 '19 at 14:03

3 Answers3

4

This is for the same reason that there is no operator+ for std::list iterators.

The standard tries not to encourage inefficient operations, in preference for you choosing the appropriate container for the job.

Creating a vector of all map keys is non-trivial because such a vector is not maintained internally along with the map.

You can of course create it if you really want, as you've already shown. But the standard wants you to know that you probably should be looking for another approach.

For example, if you just want to iterate over the keys, you can iterate over the whole map instead and use only the first part of each resultant pair.

Also, the standard library is intended to give you building blocks, not to suit every possible use case imaginable.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • well, I guess you're kind of right. maybe I should just go and program python instead... – marco Aug 06 '19 at 14:14
  • 1
    @marco Well, that would be a bit of an over-reaction, wouldn't it? Please don't misunderstand: if you need this, and you've weighed up the performance implications and found them acceptable, then by all means go ahead and do it. But you also asked why the standard didn't make this function for you. – Lightness Races in Orbit Aug 06 '19 at 14:19
2

The two chief reasons are that most implementations do not have a vector of keywords intenally, so they would have to create it on the fly. And as you can see, creating it isn't really hard so there is little need to have it in the Standard,

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

Indeed, the keys are not accessible as such because the concern of the map class is O(log(N)) retrieval, not iteration.

As a general guideline when using the stl, choose your containers according to the use case of your application. If you want fast iteration, you can use a vector of tuple<Key, Value> and sort it, or use a pair<vector<Key>, vector<Value>> that you keep sorted by hand. std::make_heap and the like can be handy for this approach.

Retrieving the map keys can be done like your implementation.

xtofl
  • 40,723
  • 12
  • 105
  • 192