Iterator question...

Discussion in 'C++' started by barcaroller, Nov 18, 2009.

  1. barcaroller

    barcaroller Guest

    Can anyone tell me why the first block of code fails to compile, while the
    second works fine.


    Block 1
    =======

    vector<int> myvector;
    string mystr;

    void foo( vector<int>::const_iterator begin,
    vector<int>::const_iterator end )
    {
    myvector.assign(begin, end);
    }

    //
    // Compiler error; string iterators not accepted as vector iterators
    //
    foo(mystr.begin(), mystr.end());


    gcc output
    -----------
    error: conversion from '__gnu_cxx::__normal_iterator,
    std::allocator > >' to non-scalar type
    '__gnu_cxx::__normal_iterator > >' requested


    Block 2
    =======

    //
    // But this works; string iterators accepted as vector iterators
    //
    myvector.assign(mystr.begin(), mystr.end());
    barcaroller, Nov 18, 2009
    #1
    1. Advertising

  2. barcaroller wrote:
    > Block 1
    > =======
    >
    > vector<int> myvector;
    > string mystr;
    >
    > void foo( vector<int>::const_iterator begin,
    > vector<int>::const_iterator end )
    > {
    > myvector.assign(begin, end);
    > }
    >
    > //
    > // Compiler error; string iterators not accepted as vector iterators
    > //
    > foo(mystr.begin(), mystr.end());


    The compiler error says it all.

    > Block 2
    > =======
    >
    > //
    > // But this works; string iterators accepted as vector iterators
    > //
    > myvector.assign(mystr.begin(), mystr.end());


    No, they are not accepted as vector iterators. They are accepted as
    _string_ iterators. Note that 'vector<>::assign' is a template method.
    When you call it with string iterators as parameters, a dedicated
    version for string iterators is instantiated.

    The same thing with an intermediate 'foo' function would look as follows

    void foo(string::iterator begin, string::iterator end)
    {
    myvector.assign(begin, end);
    }

    ...

    foo(mystr.begin(), mystr.end());

    which is what you apparently tried to do in your "Block 1", but for some
    reason you used vector iterators as parameters.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 19, 2009
    #2
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,414
  2. greg
    Replies:
    6
    Views:
    453
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    640
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,150
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,037
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page