The std::vector has two corresponding constructors:
vector(std::initializer_list<T>) [C ++ 11]vector(InputIterator first, InputIterator last) [C ++ 98]
The first is the new C ++ 11 constructor, which allows you to do things like:
std::vector<int> v{ 1, 2, 3 };
The second allows you to do things like:
std::vector<int> w{ v.rbegin(), v.rend() };
I don’t see a way to use the initializer_list constructor (since you don’t have any elements available up), so it’s best to create a key_iterator class that works on std::map<T, K>::iterator and returns (*i).first instead of (*i) . For instance:
std::vector<int> keys{ key_iterator(m.begin()), key_iterator(m.end()) };
It also requires that you key_iterator class, which you can use the Boost iterator adapters to simplify the task. It may be easier to just use the 2-line version.
reece source share