iota

B

bob

I want to populate a vector<int> with random values. I was on my to
doing this;

using namespace std;
vector<int> v( 10000);
vector<int>::iterator begin = v.begin();
vector<int>::iterator end = v.end();

iota(begin, end, 2);
random_shuffle(begin,end);


but it looks like "iota" is an old legacy thang not really in the stl.

What's the easiest/quickest way to fill a vector up with random values.


thanks very much.

G
 
V

Victor Bazarov

I want to populate a vector<int> with random values. I was on my to
doing this;

using namespace std;
vector<int> v( 10000);
vector<int>::iterator begin = v.begin();
vector<int>::iterator end = v.end();

iota(begin, end, 2);
random_shuffle(begin,end);


but it looks like "iota" is an old legacy thang not really in the stl.

What's the easiest/quickest way to fill a vector up with random values.

Something like

std::generate(v.begin(), v.end(), rand);

V
 
M

Mike Wahler

I want to populate a vector<int> with random values. I was on my to
doing this;

using namespace std;
vector<int> v( 10000);
vector<int>::iterator begin = v.begin();
vector<int>::iterator end = v.end();

iota(begin, end, 2);
random_shuffle(begin,end);


but it looks like "iota" is an old legacy thang not really in the stl.

No, there's no function 'iota()' (or 'itoa()') in the C++
(or C) standard library, nor has there ever been. But I'm
wondering: if there were such a function, why do you think
its parameters are iterators? IOW you're guessing. Don't
guess. If you envision a function you'd find useful, find
out if it exists, and *first* read its specification before
trying to use it. If it turns out it doesn't exist you'll
need to write it yourself. If you get stuck with that,
you can ask here for help.
What's the easiest/quickest way to fill a vector up with random values.

"Easy" depends upon your perception and skill level, 'quickest'
will depend upon your chosen algorithm and the 'quickness' of
the host platform.

Suggestion: Investigate 'std::generate()' from <algorithm>

-Mike
 
T

TB

(e-mail address removed) sade:
I want to populate a vector<int> with random values. I was on my to
doing this;

using namespace std;
vector<int> v( 10000);
vector<int>::iterator begin = v.begin();
vector<int>::iterator end = v.end();

iota(begin, end, 2);
random_shuffle(begin,end);


but it looks like "iota" is an old legacy thang not really in the stl.

What's the easiest/quickest way to fill a vector up with random values.


thanks very much.

G

std::vector<int> v;
std::generate_n(std::back_inserter(v),10000,rand);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top