如何获得随机指针形式的指针列表?

Dam*_*mir 0 c++ stl

如何获得随机指针形式的指针列表?我有简单的自定义class Buildings和列表

std::list<Buidldings*> temp;
Run Code Online (Sandbox Code Playgroud)

temp的大小大于零.如何从列表中获取随机指针(0 - temp.size)?

Fre*_*abe 9

使用std::rand挑选一个合适的索引,然后提前一个迭代器:

assert( !temp.empty() );
std::list<Buidldings*>::const_iterator it = temp.begin();
std::advance( it, std::rand() % temp.size() );
Buidldings *p = *it;
Run Code Online (Sandbox Code Playgroud)