Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iota

Reply
 
 
bob@blah.com
Guest
Posts: n/a
 
      01-18-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-18-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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


 
Reply With Quote
 
TB
Guest
Posts: n/a
 
      01-18-2006
(E-Mail 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);

--
TB @ SWEDEN
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-18-2006
TB wrote:
> (E-Mail 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;


Add

v.reserve(10000);

here and it might even get a bit faster...

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


V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Simulating for loop iteration with iota Victor Bazarov C++ 2 01-31-2013 04:29 AM



Advertisments