How to erase element from one vector and pass this erased value to a new vector

hi,

I am trying to figure out how to eliminate one element in one vector and then pass this value to another vector. In the assignment, there are 5 players where one of them is you as a player and 4 other computer players. I need to pass unnecessary card to player 1, and then player 1 passes one card to player 2 and so on. That means that I am player0, and I will be passed the 7th card from player4.

I got this following code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 #include <string>
#include <iostream>
#include <deque>
#include <vector>
#include <random>
#include <algorithm>
#include <ctime>

using namespace std;

const string suit[] = {"D", "H", "S", "C"};
const string facevalue[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

deque<string> the_deck =[] {
    deque<string> result;
    for (int i = 0 ; i < 52 ; ++i) {
        result.push_back(facevalue[i % 13] + suit[i % 4]);
    }
    auto eng = default_random_engine(random_device()());
    srand(time(0));
    shuffle(begin(result), end(result), eng);
    return result;
}();

string getCard () {
    auto result = the_deck.front();
    the_deck.pop_front();
    return result;
}

void print_hand(vector<string>& hand) {
    char choice = 'a';
    auto sep = "";
    for(auto& card : hand) {
        cout << sep << "(" << choice << ")" << card;
        sep = " ";
        ++choice;
    }
}

using hand_type = vector<string>;
using player_hands_type = vector<hand_type>;

int main () {
    const int DEAL_CARDS = 7; //number of cards we can deal to each player
    const int PLAYERS = 5; //number of players
    player_hands_type hands;
    srand(time(0));
    for (int player = 0 ; player < PLAYERS ; ++player) {
        hand_type hand;
        generate_n(back_inserter(hand), DEAL_CARDS, getCard);
        hands.push_back(move(hand));
    }
    print_hand(hands[0]);
    cout << "\n\nWhich one to replace? ";


    return 0;
}



Thank you so much
Last edited on
Erase?
http://www.cplusplus.com/reference/vector/vector/erase/

1. bar.push_back( foo's element x )
2. foo.erase( element x )


Edit: Move?
http://www.cplusplus.com/reference/algorithm/move/
Last edited on
Thanks a lot. I am also wondering how to iterate between those vectors. So these cards are being passed between the players.
1
2
3
4
5
6
constexpr size_t N = 7;
int array[N] {};
for (size_t k = 0; k < 42; ++k ) // merry-go-round?
{
  array[ k ] = array[ (k+1)%N ];
}
Last edited on
Do you mind explaining me what those magic numbers mean, I don't quite understand what u are trying to do in that code.
The code as such does nothing sensible.
However, the loop iterates over "cyclic array" multiple times.

* Replace 7 with actual number of players
* Replace 42 with how many times cards are passed
* Replace "int" with "hand of a player"
* Replace assignment with "move card to next player"

Was that the "iterate between vectors" question?
Thanks man for your response again. The times cards are passed are indeterminate till one of the players wins( till one of the players collects all 7 of the same suites of cards). So should I create a function and use it in for loop?
Topic archived. No new replies allowed.