0

I would like to know how I can move elements from one vector to another like for example deck vector has ace, hearts, spades. Can I move hearts from the deck vector to the player vector. I doing c++ programming.

vector <string> deck {"ace", "spade", "hearts"};
vector <string> player1 {};
sqlcoder
  • 27
  • 8
  • `player1 = deck`? What have you tried? What should the result be? – KamilCuk Mar 09 '20 at 12:57
  • 1
    @KamilCuk he asked for a `move` not a `copy` – solid.py Mar 09 '20 at 12:57
  • 1
    @KamilCuk OP asked to "move hearts". I suppose that indicates moving not all the `deck` contents. – Daniel Langr Mar 09 '20 at 12:58
  • 1
    Something like `player1.push_back(std::move(deck[2]));`, plus `deck.resize(2);`? Note that due to _small string optimization_ (provided by all major implementations), move will likely not be faster than copy. – Daniel Langr Mar 09 '20 at 13:01
  • the result should be for example moving hearts to player 1 vector. The outcome should update the vector and show that player 1 has hearts. It's not copy, it's move, I haven't tried anything yet due to not knowing what to exactly do. – sqlcoder Mar 09 '20 at 13:02
  • @DanielLangr thanks I used what you said it works fine. – sqlcoder Mar 09 '20 at 13:11

1 Answers1

1

I'd use the swap() function. Something like this should work:

deck.swap(player1)

Hope it works!