473,503 Members | 11,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10132
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
12318
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
5337
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
2643
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
2357
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
24961
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
8575
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
4484
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
12033
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
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.