简体   繁体   中英

How can I randomly iterate over each element of vector without repetition

I tried using rand() with srand() to generate random index numbers but i still get indices repeating is there any other way of iterating over all elements of a vector buy getting the indices randomly without affecting order of vector elements I cant random_shuffle() because random shuffle changes the order of my vector elements whose order is critical.

Use std::iota to fill a vector with indices. Then use std::shuffle to shuffle the index vector. Use the index vector to iterate your main vector.

Implementation left as practice to the OP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM