pre-allocated container vs. insert_iterator

Discussion in 'C++' started by yuvalif@gmail.com, Apr 9, 2008.

  1. Guest

    Hi,
    I wanted to create a simple example of why using insert_iterator and
    not a pre-allocated container. When writing a function that should
    fill data into someone else's container.
    I would be happy to hear your comments on the example (or if you have
    a better one...):

    #include <list>
    #include <iterator>
    #include <iostream>
    #include <stdlib.h>

    template <typename T, T (*Generate)(), typename OutputIterator>
    struct populate
    {
    void Do(unsigned int n, OutputIterator it)
    {
    while (n > 0)
    {
    *it = Generate();
    ++it;
    --n;
    }
    }

    void Do(unsigned int n, OutputIterator it_start, const
    OutputIterator it_end)
    {
    while (n > 0)
    {
    *it_start = Generate();
    ++it_start;
    --n;
    if (it_start == it_end)
    {
    std::cout << "iterator overflow" <<
    std::endl;
    return;
    }
    }
    }
    };

    float generate_random()
    {
    return rand();
    }

    void with_allocated_container()
    {
    typedef std::list<float> FloatList;
    typedef std::list<float>::iterator FloatListIterator;
    typedef populate<float, generate_random, FloatListIterator>
    populate_float_list;

    FloatList my_list;

    for (unsigned int i = 0; i < 10; ++i)
    {
    my_list.push_back(0.0);
    }

    FloatListIterator it = my_list.begin();
    populate_float_list my_populate;
    my_populate.Do(15, it, my_list.end());

    for (it = my_list.begin(); it != my_list.end(); ++it)
    {
    std::cout << *it << std::endl;
    }
    }

    void with_inserter()
    {
    std::cout << "with_inserter" << std::endl;
    typedef std::list<float> FloatList;
    typedef std::list<float>::iterator FloatListIterator;
    typedef std::insert_iterator< std::list<float> >
    FloatListInserter;
    typedef populate<float, generate_random, FloatListInserter>
    populate_float_list;

    FloatList my_list;

    FloatListIterator it = my_list.begin();
    populate_float_list my_populate;
    my_populate.Do(15, std::inserter(my_list, it));

    for (it = my_list.begin(); it != my_list.end(); ++it)
    {
    std::cout << *it << std::endl;
    }
    }


    int main()
    {
    with_inserter();
    with_allocated_container();
    return 0;
    }
    , Apr 9, 2008
    #1
    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. Rocky Moore
    Replies:
    7
    Views:
    1,715
    mikeb
    Jan 14, 2004
  2. =?Utf-8?B?S2V2aW4gQnVydG9u?=

    Pre-Send Request Headers, Pre-Send Request Content

    =?Utf-8?B?S2V2aW4gQnVydG9u?=, Dec 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,028
    =?Utf-8?B?S2V2aW4gQnVydG9u?=
    Dec 31, 2004
  3. Wladimir Borsov
    Replies:
    7
    Views:
    462
    Raymond Loman
    May 5, 2004
  4. Replies:
    5
    Views:
    606
    Matt Wharton
    Dec 9, 2004
  5. , India
    Replies:
    17
    Views:
    466
    Alf P. Steinbach /Usenet
    Aug 10, 2010
Loading...

Share This Page