简体   繁体   English

list :: iterator上的算术运算?

[英]arithmetic operation on list::iterator?

I got a list like this: 我得到了这样的list

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: 而且我知道l有10个元素,我想从l获取前7个元素并将它们分配给vector ,所以我尝试这样做:

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? 上面的代码无法编译,后来我发现, list不支持随机访问,vector支持,所以list::iterator不支持算术运算?

If so, How could I finish the job mentioned above? 如果是这样,我如何完成上述工作?

Use copy_n: 使用copy_n:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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