Best way to split a vector into two smaller arrays?

前端 未结 4 1723
一个人的身影
一个人的身影 2021-02-01 16:01

What I\'m trying to do:

I am trying to split a vector into two separate arrays. The current int vector contains an element per line in a text file. Th

相关标签:
4条回答
  • 2021-02-01 16:19

    If you only need a reference to the numbers without manipulating them, then you can do:

    int *array_1 = &lines[0];
    int *array_2 = &lines[lines.size() / 2];
    

    array_1 and array_2 are, actually, pointers to the start and middle of the vector. This works since STL guarantees that vectors store their elements within a continuous memory. Note that referring to lines.begin() can't be used for this.

    0 讨论(0)
  • 2021-02-01 16:28

    Use iterators.

    std::vector<int> lines;
    // fill
    std::size_t const half_size = lines.size() / 2;
    std::vector<int> split_lo(lines.begin(), lines.begin() + half_size);
    std::vector<int> split_hi(lines.begin() + half_size, lines.end());
    

    Since iterator ranges represent half open ranges [begin, end), you don't need to add 1 to the second begin iterator: lines.begin() + half_size isn't copied to the first vector.


    Note that things like

    int split = lines.size() / 2;
    int arrayA[split];
    int arrayB[split];
    

    Are not standard C++ (and as such not portable). These are so-called variable-length arrays (VLAs for short) and are a C99 thing. Some compilers have them as an extension while compiling C++ code (GCC, Clang). Always compile with -pedantic to get a warning. These VLAs act funky for non-POD types and aren't generally useful, since you can't even return them.

    0 讨论(0)
  • 2021-02-01 16:28

    If you can't use code from Xeo answer due to strict compiler rules or you want more generic way, try std::advance:

    #include <vector>
    #include <iterator>
    
    size_t middle = input.size()/2;
    std::vector<int>::const_iterator middleIter(input.cbegin());
    std::advance(middleIter, middle);
    
    std::vector<int> leftHalf(input.begin(), middleIter);
    std::vector<int> rightHalf(middleIter, input.end());
    
    0 讨论(0)
  • 2021-02-01 16:34

    Solution to split vector to variable count parts using iterator.

    #include <iostream>
    #include <vector>
    
    int main()
    {
       // Original vector of data
       std::vector<double> mainVec{1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0};
       // Result vectors
       std::vector<std::vector<double>> subVecs{};
       // Start iterator
       auto itr = mainVec.begin();
       // Variable to control size of non divided elements
       unsigned fullSize = mainVec.size();
       // To regulate count of parts
       unsigned partsCount = 4U;
       for(unsigned i = 0; i < partsCount; ++i)
       {
           // Variable controls the size of a part
           auto partSize = fullSize / (partsCount - i);
           fullSize -= partSize;
           // 
           subVecs.emplace_back(std::vector<double>{itr, itr+partSize});
           itr += partSize;
       }
       // Print out result
       for (const auto& elemOuter : subVecs)
       {
           std::cout << std::fixed;
           for (const auto& elemInner : elemOuter)
           {
               std::cout << elemInner << " ";
           }
           std::cout << "\n";
       }
    }
    
    0 讨论(0)
提交回复
热议问题