I would like to know how I can erase an element in a vector randomly. For example I have a vector which has integers 1,2,3,4,5 in it. I want to erase one of those randomly. How would I do such a thing?
int main() {
vector <int> myvec {1,2,3,4,5};
if (!myvec.empty()) {
myvec.erase(myvec.begin());
cout << myvec.at(0);
cout << myvec.at(1);
cout << myvec.at(2);
cout << myvec.at(3);
}
else {
cout << "Vector is empty" << endl;
}
}
Above is the code I have so far. It is an if and else statement which checks if the vector is empty or not and erases an element. The issue is I'm not sure how to get it to remove a random element. At the moment it only removes the first element.