简体   繁体   English

C++ 将向量向量的元素推入向量中

[英]C++ pushing elements of a vector of vectors into a vector

I'm just wondering whether there is a simple, quick way to insert the elements of a vector of vectors inside a vector.我只是想知道是否有一种简单快捷的方法可以在向量中插入向量向量的元素。

For example:例如:

std::vector<std::vector<double> > vals
{

    {1, 2, 3, 4, 5, 6},
    {1, 2, 3, 4, 5, 6}
};
std::vector<double> myvector;

so myvector would then contain: 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6所以myvector将包含: 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6

I've tried using insert and push_back but had no luck.我试过使用insertpush_back但没有运气。

Edit:编辑:

I have used:我用过了:

myvector.insert(vals.begin(), vals.end()); - No matching member function to call to 'insert' - 没有匹配的成员函数来调用“插入”

Try尝试

// c++11
for(auto && v : vals){
  myvector.insert(myvector.end(), v.begin(), v.end());
}

// non c++11
for(std::vector<std::vector<double> >::const_iterator it = vals.begin(), end = vals.end(); it != end; ++it){
  myvector.insert(myvector.end(), it->begin(), it->end());
}

Depending on how your vector of vectors is built in the first place, there may be a more efficient way to approach this.取决于你的向量向量是如何构建的,可能有一种更有效的方法来解决这个问题。

If your sub-vectors are all the same length (as appears to be the case from the code example), then the best thing may be to view myvector as a 2D array and build directly into this buffer.如果您的子向量的长度都相同(就像代码示例中的情况一样),那么最好的方法可能是将 myvector 视为二维数组并直接构建到此缓冲区中。

If the sub-vector lengths can vary, but the code that builds your vector of vectors only ever actually adds stuff at the end , then you can use something like the 'collapsed vector vector' class I describe here to build this data, instead of a vector of vectors.如果子向量长度可以变化,但是构建向量向量的代码实际上只会在最后添加内容,那么您可以使用我在此处描述的“折叠向量向量”类来构建此数据,而不是向量的向量。 (And after that, again, your data is already in the kind of contiguous buffer you are looking for.) (在那之后,您的数据已经在您正在寻找的那种连续缓冲区中。)

Either way, if performance or memory footprint are important here then avoiding the vector of vector construct can eliminate a lot of buffer manipulation overhead, and work out as quite a significant optimisation.无论哪种方式,如果性能或内存占用在这里很重要,那么避免向量构造的向量可以消除大量缓冲区操作开销,并作为一个非常重要的优化。

If you want to insert all of the elements of vals into a single vector:如果要将vals所有元素插入到单个向量中:

Brute Force Method蛮力法

for (int i = 0; i < vals.size(); ++i)
{
    for (int j = 0; j < vals[i].size(); ++j)
    {
        myvector.push_back(vals[i][j]);
    }
}

More Elegant Method更优雅的方法

struct AddVector
{
    std::vector<double> myvector;
    void operator()(const std::vector<int>& a)
    {
        std::copy(a.begin(), a.end(), std::back_inserter(myvector));
    }
};

// somewhere in your code
AddVector av;
std::for_each(vals.begin(), vals.end(), av);
myvector.swap(av.myvector);

The variable vals is a vector of vectors, but myvector is a vector of doubles.变量vals是向量的向量,而myvector是双精度向量。 Trying to insert from the first into the second will not work as the contained types are different ( std::vector<double> versus double ).尝试从第一个插入到第二个将不起作用,因为包含的类型不同( std::vector<double>double )。

Instead you have to manually insert from each sub-vector of vals into myvector .相反,您必须手动将vals每个子向量插入到myvector

resize myvector to the size of the sum of the sizes of the vectors in vals.将 myvector 的大小调整为 vals 中向量大小之和的大小。 For every vector in vals concat it to myvector.对于 vals 中的每个向量,将其连接到 myvector。 To get a idea how to concat:要了解如何连接:

Concatenating two std::vectors 连接两个 std::vectors

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

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