0

I have a std::map<int, int> and a std::list<std::map<int, int>::const_iterator>.

Can I use an STL-algorithm to fill the list with all the iterators from the map in order?
I don't want to use an explicit loop if possible.

4
  • You can't do this without a loop being involved. Even std::copy() uses a loop. Commented Nov 2, 2014 at 16:42
  • @Galik Presumably he means explicit loops. Commented Nov 2, 2014 at 16:43
  • @Galik: Yes. I thought about nice written code. In one line using STL. I know that inside I need to use loop by I want to hide it. Commented Nov 2, 2014 at 16:44
  • I think I'd take a step back. Why, exactly, do you believe you need a std::list<map<int, int>::iterator>? This sounds highly suspect to me. Commented Nov 2, 2014 at 16:45

1 Answer 1

7

The algorithms in the STL call functors with values, not with iterators. If you are obsessed with avoiding explicit loops then you might try to use iota with an iterator as a value:

list.resize(map.size()); std::iota( std::begin(list), std::end(list), std::begin(map) ); 

Demo.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.