473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

iota

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

Jan 18 '06 #1
4 10126
bo*@blah.com 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
Jan 18 '06 #2
"bo*@blah.com" <Gr**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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
Jan 18 '06 #3
TB
bo*@blah.com 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
Jan 18 '06 #4
TB wrote:
bo*@blah.com 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
Jan 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
2
by: Kyle | last post by:
Got a tough one here for you SQL junkies. I'm working on a website (in ASP) for a national greek/college organization. All it's college chapters have greek chapter names, i.e. Alpha Chapter,...
4
by: Christopher | last post by:
This should be a quick one. URL: http://cfa-www.harvard.edu/~cpilman/Stuff/flush.html Code: ============================= <!DOCTYPE HTML Public "-//W3C//DTD HTML 4.01//EN">...
29
by: Flzw | last post by:
Alright, here is a simple function I coded, won't be hard understanding what it does, and YES, I know, you will probably tell me it's awful code, I would like to know how to write it better, maybe...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
4
by: fightwater | last post by:
i wrote a program which uses the stl compose1 functor but the compiler seems cannot find it here is the code ////////////////////////////////////////// #include <vector> #include <numeric>...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.