简体   繁体   中英

arithmetic operation on list::iterator?

I got a list like this:

list<float> l;

And I know there are 10 elements in l , I want to take first 7 elements from l and assign them to a vector , so I tried to do it like this:

vector<float> v(l.begin(), l.begin()+7);

The code above can't compile, later I found out that, list doesn't support random access while vector does, so list::iterator doesn't support arithmetic operation?

If so, How could I finish the job mentioned above?

Use copy_n:

v.resize(7);
copy_n(l.begin(), 7, v.begin());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM