C ++将向量的向量分割为向量的N个子向量

时间:2018-11-01 02:02:28

标签: c++ c++11 vector split

我有一个非常大的向量向量,我想通过一个函数将其分成N个(向量的)子向量。然后,该函数将在这些子向量上执行一些算法例程,但不返回任何内容。我知道要将向量的原始向量分割成多少个向量(尽管不是在编译时),而且我不确定如何在向量中创建N个向量的子向量在运行时起作用。

通常,如果要将向量拆分为n个子向量,则将创建向量向量来存储每个子向量,尤其是在您要求这些子向量的范围超出循环范围的情况下,尤其如此正在用于执行拆分。我是否正在寻找(缺乏更好的描述)“四维向量”来存储向量的这些子向量?

为澄清起见,假设我有一个向量向量,如下所示:

vec = { {945,1,1.0882222739646},
        {955,1,1.08030633720477},
        {965,1,1.06095611392935},
        {975,1,1.0736443050851},
        {985,1,1.04649065403142},
        {995,1,1.06294684603874},
        {1005,1,1.065654589561},
        {1015,1,1.0668922119373},
        {1025,1,1.03109676962124},
        {1035,1,1.08430139146623} }

我想将其拆分为5个向量的子向量(在运行时确定),如下所示:

vec1 = { {945,1,1.0882222739646},
         {955,1,1.08030633720477} }

vec2 = { {965,1,1.06095611392935},
         {975,1,1.0736443050851} }

vec3 = { {985,1,1.04649065403142},
        {995,1,1.06294684603874} }

vec4 = { {1005,1,1.065654589561},
        {1015,1,1.0668922119373} }

vec5 = { {1025,1,1.03109676962124},
        {1035,1,1.08430139146623} }

这到底是怎么回事?到目前为止,我的功能如下:

void calc_frac_block (vector<vector <double> > conc_data, vector<int> expidx)
{
   // First, we need to create n-many vectors of vectors corresponding to the size of expidx
   int expidx_size = expidx.size();

   cout << "Size of expidx is: " << expidx_size << endl;

   // Now we find the size of each subvector of vectors by diving conc_data by expidx_size
   int subvec_size = conc_data.size() / expidx_size;   // Will always be a whole number

   cout << "Size of each subvector is: " << subvec_size << endl;

   // NOW I HAVE THE NUMBER OF SUB-VECTORS OF VECTORS AND THE SIZE OF EACH... HOW TO PROCEED?

}

任何指针都会有所帮助。我正在使用C ++ 11,因此可以接受其中包含功能的解决方案;)

1 个答案:

答案 0 :(得分:2)

将向量分割成一个循环

vector<vector<vector <double>>> sub_vectors;
for (std::size_t i(0); i < expidx_size; ++i) {
    sub_vectors.emplace_back(vec.begin() + i * subvec_size, vec.begin() + (i + 1) * subvec_size);
}

这是示例代码:

#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<double>> vec = { {945,1,1.0882222739646},
        {955,1,1.08030633720477},
        {965,1,1.06095611392935},
        {975,1,1.0736443050851},
        {985,1,1.04649065403142},
        {995,1,1.06294684603874},
        {1005,1,1.065654589561},
        {1015,1,1.0668922119373},
        {1025,1,1.03109676962124},
        {1035,1,1.08430139146623} };

    int expidx_size(5);
    std::cout << "Size of expidx is: " << expidx_size << std::endl;

    int subvec_size = vec.size() / expidx_size;

    std::cout << "Size of each subvector is: " << subvec_size << std::endl;

    std::vector<std::vector<std::vector <double>>> sub_vectors;
    for (std::size_t i(0); i < expidx_size; ++i) {
            sub_vectors.emplace_back(vec.begin() + i * subvec_size, vec.begin() + (i + 1) * subvec_size);
    }

    for (std::size_t i(0); i < expidx_size; ++i) {
        for (const auto& sub_vector : sub_vectors[i]) {
            std::cout << "{ ";
            for (const auto& value : sub_vector) {
                std::cout << value << " ";
            }

            std::cout << " }";
        }
        std::cout << std::endl;
    }
    return 0;
}