About 5,210,000 results
Open links in new tab
    Upvotes14Top Answeredited Dec 15, 2012 at 0:24

    If you really need 2 vectors, and you can't use GMan's suggestion in the comments:

    // where v1 is your original vector
    std::vector<T> v2(
    std::make_move_iterator(v1.begin() + v1.size()/2),
    std::make_move_iterator(v1.end()));
    v1.erase(v1.begin() + v1.size()/2, v1.end());

    It's still O(n), but you can't do any better than that.

    If you need to keep the original vector separate:

    std::vector<T> v2(v1.begin(), v1.begin() + v1.size()/2),
    v3(v1.begin() + v1.size()/2, v1.end());
    Content Under CC-BY-SA license
    Was this helpful?
  1. Slicing a Vector in C++ - GeeksforGeeks

  2. Question & Answer
  3. Split a Vector into Multiple Smaller Vectors in Java - GeeksforGeeks

    Tags:
    Split A Vector
    Java
  4. Best way to split a vector into two smaller arrays?

    Tags:
    Split A Vector
    Split Vector Into Two Vectors C
    Split The Array in C
  5. Split a vector into sub-vectors of size n in C++ - Techie …

    May 16, 2024 · Splitting a vector into sub-vectors of a specific size is very easy in C++. We start by determining the total number of sub-vectors of size n formed from the input vector.

    Tags:
    Split A Vector
    C++
  6. C++容器vector的数组片段截取操作 - CSDN博客

  7. Smart way to split a vector into smaller - C++ Forum - C++ Users

    Tags:
    Std Vector Split
    Split Vector Into Two Vectors C
    C String Split Into Vector
  8. Split a vector into two equal parts in C++ | Techie Delight

    Tags:
    Split A Vector
    C++
  9. How can I split a vector? - MATLAB Answers - MATLAB Central

    Tags:
    Split A Vector
    Matlab Split Vector
  10. NumPy Splitting Array - W3Schools