iota

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

  1. bob

    bob 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
     
    bob, Jan 18, 2006
    #1
    1. Advertisements

  2. Something like

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

    V
     
    Victor Bazarov, Jan 18, 2006
    #2
    1. Advertisements

  3. bob

    Mike Wahler Guest

    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.
    "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. bob

    TB Guest

    sade:
    std::vector<int> v;
    std::generate_n(std::back_inserter(v),10000,rand);
     
    TB, Jan 18, 2006
    #4
  5. Add

    v.reserve(10000);

    here and it might even get a bit faster...
    V
     
    Victor Bazarov, Jan 18, 2006
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
Loading...