iota

Discussion in 'C++' started by bob@blah.com, Jan 18, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jan 18, 2006
    #2
    1. Advertising

  3. Mike Wahler Guest

    "" <> wrote in message
    news:...
    >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
     
    Mike Wahler, Jan 18, 2006
    #3
  4. TB Guest

    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
     
    TB, Jan 18, 2006
    #4
  5. TB wrote:
    > 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
     
    Victor Bazarov, Jan 18, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    2
    Views:
    209
Loading...

Share This Page