从向量中提取子向量的最佳方法? [英] Best way to extract a subvector from a vector?
本文介绍了从向量中提取子向量的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个大小 std :: vector
(我们称之为 myVec
> N 。构造由元素X到Y的副本构成的新向量的最简单方法是什么,其中0≤X≤Y≤N-1≤Y≤N。例如,在大小 150000的向量中的
myVec [100000]
到 myVec [100999]
Suppose I have a std::vector
(let's call it myVec
) of size N
. What's the simplest way to construct a new vector consisting of a copy of elements X through Y, where 0 <= X <= Y <= N-1? For example, myVec [100000]
through myVec [100999]
in a vector of size 150000
.
如果使用向量无法有效地完成此操作,是否需要使用另一个STL数据类型?
If this cannot be done efficiently with a vector, is there another STL datatype that I should use instead?
推荐答案
vector<T>::const_iterator first = myVec.begin() + 100000;
vector<T>::const_iterator last = myVec.begin() + 101000;
vector<T> newVec(first, last);
这是一个O(N)操作来构造新的向量,方式。
It's an O(N) operation to construct the new vector, but there isn't really a better way.
这篇关于从向量中提取子向量的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文