Hello everybody. I'm trying to figure out how it's possible to remove an element (by its value) from an array. I came across <list> and this is my current code:
#include <iostream>
#include <list>
#include <stdio.h>
usingnamespace std;
int main()
{
srand ( time(NULL) );
int cards[] = {1,1,2,2,5,5,6,5};
list<int> card (cards,cards+8);
card.remove(1);
cout << "Deleting cards with value 1. Cards now contains:";
for (list<int>::iterator it=card.begin(); it!=card.end(); ++it)
cout << " " << *it;
cout << endl;
card.remove(2);
cout << "Deleting cards with value 2. Cards now contains:";
for (list<int>::iterator it=card.begin(); it!=card.end(); ++it)
cout << " " << *it;
cout << endl;
cout << "Random card: " << cards[rand()%8] << endl;
system("PAUSE");
}
Now, at the end, it's still giving me the numbers 1 and 2 on random occassion. But I deleted them, didn't I? I want the program to randomize from only 5 and 6 now, since they should be the only elements within the array.
What am I doing wrong/How is this possbible to be executed?
1) Did you mean card on line 29?
2) std::list doesn't have an operator [] defined
3) If you want a random card, it could be easier to just use std::random_shuffle and then just go through the list in order