-1

I'm making blackjack and I'm trying to erase cards from the deck vector after dealing them, there is an error for every single instance of the .erase function. Im a beginner so sorry if this is something trivial.

#include <bits/stdc++.h>
#include <random>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main() {
  
  std::vector<int>deck {2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11}; 
   std::random_device rd;
    std::mt19937 g(rd());
  std::shuffle(deck.begin(), deck.end(), g);
  //creates 2 decks and shuffles them
  int dealervalue = deck[0];
  deck.erase(0);  
  int yourvalue = deck[0] + deck[1];
  deck.erase(0);
  deck.erase(0);  
  std::cout << dealervalue << "\n" << yourvalue;
  //deals cards
return 0;
 }
  • 5
    The error is self-explanatory: [`std::vector::erase`](https://en.cppreference.com/w/cpp/container/vector/erase) has no overload that takes an `int` argument. They all require iterators (like `deck.begin()` for the card you're apparently pulling). And honestly, `std::deque` would be a better container if I understand your intended use case. – WhozCraig Jan 13 '22 at 18:24
  • Probably a noob question but are you telling me that .erase doesn't take an int arguement? If so how do I erase an element from a vector at a specific position? @WhozCraig – Samuel Frutkin Jan 13 '22 at 18:28
  • Yes, that's what I'm telling you (more to the point that's what your *compiler* it telling you; I'm just pointing you to the docs to explain why). Look at the example I linked. It documents both `erase` *and* even shows an example of how to 'pop' the front element of a vector. – WhozCraig Jan 13 '22 at 18:29
  • 3
    Unrelated: The use of `#include ` along with the other include directives suggests you don't understand what bits/stdc++.h does. [Here's a bit of reading on that](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Jan 13 '22 at 18:34
  • Expanding on WhozCraig's other point, `vector` sucks at removal from the front or anywhere but the back. Everything after the removed item needs to be moved forward one slot to fill up the space. `deque`, double-ended queue, stores the data very differently and makes it very cheap (and easy: see [`pop_front`](https://en.cppreference.com/w/cpp/container/deque/pop_front)) to remove the item at the beginning (or the end), but is a bit more expensive for some other behaviours because you never get something for nothing. – user4581301 Jan 13 '22 at 18:39
  • 1
    If you model the deck of cards turned the other way round, you can pick the cards from the *end* of the vector - `int dealervalue = deck.back(); deck.pop_back(); ` – BoP Jan 13 '22 at 20:09

1 Answers1

3

The problem lies in that std::vector::erase does not take an integer argument, but rather an iterator to an element in the vector. To erase the first element of a vector, use

vector.erase(vector.begin());

And since begin() returns a random access iterator, you can remove an element at index i by calling:

vector.erase(vector.begin() + i);
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36