Thread: list<int> constructor

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    list<int> constructor

    Code:
    int data[] = {1, 2, 3, 4, 5, 6, 7, 8};
    list<int> numbers(data, data+3); // 1 2 3
    list<int> values(data+4, data+8); // 5 6 7 8
    numbers.splice(++numbers.begin(), values); // 1 5 6 7 8 2 3
    list<T> accepts an iterator but here we provide a pointeR ??? it works... wHy ???

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since a pointer behaves like an iterator (adding one to it works by giving you the next piece of memory, using * works by giving you an int), then it is an iterator. (I'm getting tired of reading that line in Josuttis, so I thought I'd inflict it on you.)

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    so u mean we can assign pointers to an iterator both of the same tyPE ?

    and it will work ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That particular constructor is itself a function template whose template argument is an input iterator. Consequently, it works with any pair of input iterators of the same type, from pointers to std::istream_iterator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea what you're asking. A std::list constructor takes a very general sort of iterator (it doesn't have to be a list::iterator), and int* can be converted to that general iterator. You can't assign an int* to a list::iterator.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That constructor is templated, which means that as long as the argument types meet the requirements anything goes.

    So, that constructor might be implemented as following:
    Code:
        template <class Iter>
        list(Iter first, Iter last)
        {
            while (first != last) {
                theList.push_back(*first);
                ++first;
            }
        }
    As you can see, what is required of the Iter type is that it supports operator!=, operator* (which must return a type that can go into the list), and operator++. These operators work for both pointers and iterators.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> so u mean we can assign pointers to an iterator both of the same tyPE ?

    No, you can't assign a pointer to a list<T>::iterator. They are different types. The constructor for list doesn't take a regular type, it takes a templated type. A templated type is different than a regular type, because it is just a concept. It means, any regular type that fits certain requirements can be used here.

    So any regular type that can be incremented with ++ and can be dereferenced with * should work there. That doesn't mean anything that works there is the same type, just that they all fit the "iterator" concept, or in this case the input iterator concept.
    Last edited by Daved; 11-07-2008 at 01:22 PM. Reason: fixed iterator - thanks laserlight

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Further, because of all that, you can create lists out of things that have little to do with arrays or sequences, as long as they have a suitable interface. For example the following, which generates a sequence as demanded (the minimal example might lack a few operators):

    Code:
     
    #include <iostream>
    #include <list>
    
    class make_sequence
    {
        int value;
    public:
        make_sequence(int n): value(n) {}
        make_sequence& operator++()
        {
            ++value;
            return *this;
        }
        int operator*() const { return value; }
        bool operator!= (make_sequence other) const
        {
            return value != other.value;
        }
    };
    
    int main()
    {
        std::list<int> n(make_sequence(1), make_sequence(10)); //1, 2, 3, 4, 5, 6, 7, 8, 9
        for (std::list<int>::iterator it = n.begin(); it != n.end(); ++it) {
            std::cout << *it << ' ';
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Daved
    That doesn't mean anything that works there is the same type, just that they all fit the "iterator" concept, or in this case the forward_iterator concept.
    As I mentioned earlier, the requirement is not so strict: only input iterators are required since a copy is a single pass algorithm.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Can't find my constructor
    By Nippashish in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 08:17 PM