假设我有一个大小为n的std::vector(让我们称之为myVec),构造一个由元素X到Y的副本组成的新向量,其中0 <= X <= Y <= N-1,最简单的方法是什么?例如,大小为150000的向量中的myVec[100000]到myVec[100999]。
如果这不能有效地用一个向量,是否有另一种STL数据类型,我应该使用代替?
假设我有一个大小为n的std::vector(让我们称之为myVec),构造一个由元素X到Y的副本组成的新向量,其中0 <= X <= Y <= N-1,最简单的方法是什么?例如,大小为150000的向量中的myVec[100000]到myVec[100999]。
如果这不能有效地用一个向量,是否有另一种STL数据类型,我应该使用代替?
当前回答
投射一个不是线性时间的集合的唯一方法是惰性地这样做,其中产生的“vector”实际上是委托给原始集合的子类型。例如,Scala的List#subseq方法在常数时间内创建一个子序列。但是,只有当收集是不可变的并且底层语言支持垃圾收集时,这才有效。
其他回答
你没有提到什么类型std::vector<…> myVec是,但如果它是一个简单的类型或结构/类,不包括指针,你想要最好的效率,那么你可以做一个直接的内存复制(我认为这将比其他答案提供的更快)。下面是std::vector<type> myVec的一般示例,这里的类型是int:
typedef int type; //choose your custom type/struct/class
int iFirst = 100000; //first index to copy
int iLast = 101000; //last index + 1
int iLen = iLast - iFirst;
std::vector<type> newVec;
newVec.resize(iLen); //pre-allocate the space needed to write the data directly
memcpy(&newVec[0], &myVec[iFirst], iLen*sizeof(type)); //write directly to destination buffer from source buffer
将元素从一个向量复制到另一个向量很容易 在这个例子中,为了便于理解,我使用了一对向量 `
vector<pair<int, int> > v(n);
//we want half of elements in vector a and another half in vector b
vector<pair<lli, lli> > a(v.begin(),v.begin()+n/2);
vector<pair<lli, lli> > b(v.begin()+n/2, v.end());
//if v = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
//then a = [(1, 2), (2, 3)]
//and b = [(3, 4), (4, 5), (5, 6)]
//if v = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)]
//then a = [(1, 2), (2, 3), (3, 4)]
//and b = [(4, 5), (5, 6), (6, 7)]
' 正如你所看到的,你可以很容易地将元素从一个向量复制到另一个向量,例如,如果你想将元素从索引10复制到索引16,那么我们将使用
vector<pair<int, int> > a(v.begin()+10, v.begin+16);
如果你想让元素从索引10到末尾的某个索引,那么在这种情况下
vector<pair<int, int> > a(v.begin()+10, v.end()-5);
希望这有助于,只要记住在最后的情况下v.end()-5 > v.begin()+10
只要使用向量构造函数。
std::vector<int> data();
// Load Z elements into data so that Z > Y > X
std::vector<int> sub(&data[100000],&data[101000]);
我打赌第一个编码器现在已经完成了。 对于简单的数据类型,不需要复制,只需恢复到良好的旧C代码方法。
std::vector <int> myVec;
int *p;
// Add some data here and set start, then
p=myVec.data()+start;
然后将指针p和len传递给任何需要子向量的对象。
Notelen一定是!!len < myVec.size()-start
如果这两个都不打算修改(不添加/删除项-只要注意线程问题,修改现有项就可以),您可以简单地传递data.begin() + 100000和data.begin() + 101000,并假装它们是一个更小的向量的begin()和end()。
或者,因为矢量存储保证是连续的,你可以简单地传递一个1000项数组:
T *arrayOfT = &data[0] + 100000;
size_t arrayOfTLength = 1000;
这两种技术都需要固定的时间,但要求数据长度不增加,从而触发重新分配。