0

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.

asmmo
  • 6,922
  • 1
  • 11
  • 25
maany
  • 31
  • 7
  • If you search here for "[c++] generate random number" you will find descriptions of how to generate random values in a range (say, from `0` to `myvec.size()-1`). Generate such a value, and add it to `myvec.begin()` to obtain an iterator that references an element of `myvec`. – Peter Apr 06 '20 at 13:43
  • 2
    @maany: When learning to program, it helps to break down the problem into smaller chunks. Can you remove the 2nd element? Can you write a function that takes an `int i` and removes the i'th element? After that, you only need to call that function with a random value. – MSalters Apr 06 '20 at 13:53
  • @MSalters yes I can remove the second element by adding +1 after the begin – maany Apr 06 '20 at 13:55

3 Answers3

0

Thanks for the comments. I figured it out.

srand((unsigned) time(0));
  int randomNumber;
  for (int index = 0; index < 1; index++) {
    randomNumber = (rand() % 5) + 0;
    cout << randomNumber << endl;
  }



    vector <int> myvec {1,2,3,4,5}; 



 if (!myvec.empty()) { 


     myvec.erase(myvec.begin() + randomNumber);


     cout << myvec.at(0);
     cout << myvec.at(1);
     cout << myvec.at(2);
     cout << myvec.at(3);


 }

 else {

     cout << "Vector is empty" << endl;

     }
}

maany
  • 31
  • 7
0

Use a random integers generator to generate an integer from (0 to size of your vector -1), then use erase with the generated number, as follows

#include<random>
#include<chrono>
#include<vector>
int main()
{
    std::vector <int> myVec {1,2,3,4,5};

    //creat a random engine object and seed it 
    std::default_random_engine engineObj(std::chrono::system_clock::now().time_since_epoch().count());
    //The distribution will be used
    std::uniform_int_distribution<size_t> randomInt{0ul, myVec.size()-1};

    //Erase
    myVec.erase(myVec.begin() + randomInt(engineObj));

    //Test
    for(auto const & el: myVec) std::cout << el << " ";
    }
asmmo
  • 6,922
  • 1
  • 11
  • 25
-1

You want the rand() function. Use it to generate a random index into your vector.

  • 3
    Since 2011 that is no longer recommended. Use `#include ` instead (unless you are stuck on some really old compiler): [https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library](https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library) – drescherjm Apr 06 '20 at 13:47