简体   繁体   中英

Inserting a vector in a certain position in another vector

I want to insert vector b in a certain position in a vector a . For Example

std::vector <int> vecta{ 10, 20, 30 ,40 , 50};
std::vector <int> vectb{ 1000, 2000, 3000 };

How can I have a result vector to be like {10,20,1000,2000,3000,40,50} ? I want to remove 30 and replace its with the vector.

I want to insert vector b in a certain position in a vector a

You may want to considerstd::vector::insert() :

vecta.insert(pos, vectb.begin(), vectb.end());

where pos above is an iterator pointing to the element (in vecta ) before the content of vectb will be inserted. pos may be also the iterator returned by end() , which would mean appending the content of vectb to vecta .


How can I have a result vector to be like {10,20,1000,2000,3000,40,50}

For that, you also need to remove the element 30 from vecta . You can do this with std::vector::erase() :

auto main() -> int {
   std::vector <int> vecta{ 10, 20, 30, 40 , 50};
   /*                               ^
                                    |-- insert vectb here and replace the 30
   */

   std::vector <int> vectb{ 1000, 2000, 3000 };

   // what element to erase from vecta?
   auto pos = vecta.begin() + 2;

   // erase it
   pos = vecta.erase(pos);

   // insert vectb in vecta
   vecta.insert(pos, vectb.begin(), vectb.end());

   for (auto& e: vecta)
      std::cout << e << " ";
   std::cout << std::endl;
}

std::vector::erase() returns the iterator that follows the removed element. Since you want to erase 30 from vecta and then insert vectb at that position, you can simply pass the iterator erase() returns to insert() .

You can use erase to erase an element at a certain iterator position and insert to insert elements before a certain iterator position. To step an iterator, use std::next and std::prev .

#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> vecta{10, 20, 30, 40, 50};
    std::vector<int> vectb{1000, 2000, 3000};

    // erase 30 from the vector
    vecta.erase(std::next(vecta.begin(), 2));

    // insert vectb before position 2
    vecta.insert(std::next(vecta.begin(), 2), vectb.begin(), vectb.end());

    for(int v : vecta) {
        std::cout << v << ' ';
    }
    std::cout << '\n';
}

Output:

10 20 1000 2000 3000 40 50

An alternative that doesn't require erase . This is probably slightly faster:

#include <utility> // added for std::swap

int main() {
    std::vector<int> vecta{10, 20, 30, 40, 50};
    std::vector<int> vectb{1000, 2000, 3000};

    std::vector<int> result;

    // reserve space for the number of elements you know will be in the resulting vector
    result.reserve(vecta.size() - 1 + vectb.size());

    // append the 2 first elements from vecta
    result.insert(result.end(), vecta.begin(), std::next(vecta.begin(), 2));

    // append vectb
    result.insert(result.end(), vectb.begin(), vectb.end());

    // append the last two elements from vecta
    result.insert(result.end(), std::prev(vecta.end(), 2), vecta.end());

    // let vecta take over the data in result and vice-a-versa
    std::swap(result, vecta);
}

You can usevector::insert

Live sample

vecta.erase(vecta.begin() + 2); //<-- erase 30 element
vecta.insert(vecta.begin() + 2, vectb.begin(), vectb.end());

vecta.begin() + 2 being the point of insertion of vectb in vecta and vectb.begin(), vectb.end() the span of the elements of vectb you need to insert, In this case, all of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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