Sunday 15 August 2010

C++: Efficiently access random vector positions -



C++: Efficiently access random vector positions -

so have written code randomly access 5 positions of vector. have done populating 1 vector file, creating sec vector hold random 5 elements of first

populate 1st vector file

std::vector<std::string> elemalg::getquiz(int quizno) { std::ifstream fin("1.txt"); while (std::getline(fin, question)) { questions.push_back(question); } homecoming questions; }

access 5 random elements 1st vector , re-create them new vector

std::vector<std::string> elemalg::allocatequestions() { unsigned int seed = time(0); std::ifstream infile("rand.txt"); if (infile) { infile >> seed; infile.close(); } srand(seed); (int = 0; <= 4; i++) { seed = rand(); unsigned int randomindex = seed % questions.size(); selectedquestions.push_back(questions[randomindex]); } std::ofstream outfile("rand.txt"); outfile << seed; outfile.close(); homecoming selectedquestions; }

is relatively efficient way of doing this, need 2 vectors? or have on complicated simple problem?

is there way test efficiency of it?

thanks anyones time. (note aware duplications of randomindex possible)

c++ random vector srand

No comments:

Post a Comment