"vector<int>::iterator" problem

Discussion in 'C++' started by arnuld, Oct 12, 2006.

  1. arnuld

    arnuld Guest

    i wrote a programme to create a vector of 5 elements (0 to 4), here is
    the code & output:

    #include <iostream>
    #include <vector>

    int main() {
    std::vector<int> ivec;

    // dynamically create a vector
    for(std::vector<int>::size_type ix = 0; ix <= 4; ++ix)
    {
    ivec.push_back(ix);
    }


    // print-out the elements
    for(std::vector<int>::iterator iter = ivec.begin();
    iter != ivec.end(); ++iter)
    {
    std::cout << "Element: " << *iter << "..." << std::endl;
    }

    // create a middle iterator
    std::vector<int>::iterator mid_iter = (ivec.begin() + ivec.size() /
    2);
    // print it out to check where it points
    std::cout << "middle element: " << *mid_iter << std::endl;

    return 0;
    }

    OUTPUT:

    Element: 0...
    Element: 1...
    Element: 2...
    Element: 3...
    Element: 4...
    middle element: 2 // fine :)


    now if i change the "mid_iter" to:

    /std::vector<int>::iterator mid_iter = (ivec.size() / 2);/

    then i got an ERROR:
    ----------------------------------------------------------------
    unix@debian:~/Desktop$ g++ test.cpp

    test.cpp: In function `int main()':
    test.cpp:22: error: conversion from `unsigned int' to non-scalar type `
    __gnu_cxx::__normal_iterator<int*, std::vector<int,
    std::allocator<int> > >'
    requested

    unix@debian:~/Desktop$
    ----------------------------------------------------------------


    it is showing "non-scalar type". what is that? what is happening
    "behind the curtains"?


    "arnuld"
     
    arnuld, Oct 12, 2006
    #1
    1. Advertising

  2. arnuld

    Kai-Uwe Bux Guest

    arnuld wrote:

    > i wrote a programme to create a vector of 5 elements (0 to 4), here is
    > the code & output:
    >
    > #include <iostream>
    > #include <vector>
    >
    > int main() {
    > std::vector<int> ivec;
    >
    > // dynamically create a vector
    > for(std::vector<int>::size_type ix = 0; ix <= 4; ++ix)
    > {
    > ivec.push_back(ix);
    > }
    >
    >
    > // print-out the elements
    > for(std::vector<int>::iterator iter = ivec.begin();
    > iter != ivec.end(); ++iter)
    > {
    > std::cout << "Element: " << *iter << "..." << std::endl;
    > }
    >
    > // create a middle iterator
    > std::vector<int>::iterator mid_iter = (ivec.begin() + ivec.size() /
    > 2);
    > // print it out to check where it points
    > std::cout << "middle element: " << *mid_iter << std::endl;
    >
    > return 0;
    > }
    >
    > OUTPUT:
    >
    > Element: 0...
    > Element: 1...
    > Element: 2...
    > Element: 3...
    > Element: 4...
    > middle element: 2 // fine :)
    >
    >
    > now if i change the "mid_iter" to:
    >
    > /std::vector<int>::iterator mid_iter = (ivec.size() / 2);/
    >
    > then i got an ERROR:
    > ----------------------------------------------------------------
    > unix@debian:~/Desktop$ g++ test.cpp
    >
    > test.cpp: In function `int main()':
    > test.cpp:22: error: conversion from `unsigned int' to non-scalar type `
    > __gnu_cxx::__normal_iterator<int*, std::vector<int,
    > std::allocator<int> > >'
    > requested
    >
    > unix@debian:~/Desktop$
    > ----------------------------------------------------------------
    >
    >
    > it is showing "non-scalar type". what is that? what is happening
    > "behind the curtains"?


    Well, look at the statement:

    std::vector<int>::iterator mid_iter = (ivec.size() / 2);

    The variable mid_iter has type std::vector<int>::iterator. Note that
    std::vector<int>::iterator, according to the standard, is an implementation
    defined type. What it is or is not 'behind the curtains' is of no concern
    to the programmer.

    Now, the expression (ivec.size() / 2) has type std::vector<int>::size_type.
    The compiler is telling you that you cannot initialize an iterator from a
    size_type. The compiler it correct: the interface of iterator does not
    contain such a conversion (for good reasons!).



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 12, 2006
    #2
    1. Advertising

  3. arnuld

    yoge Guest

    Kai-Uwe Bux wrote:

    > Well, look at the statement:
    >
    > std::vector<int>::iterator mid_iter = (ivec.size() / 2);
    >
    > The variable mid_iter has type std::vector<int>::iterator. Note that
    > std::vector<int>::iterator, according to the standard, is an implementation
    > defined type. What it is or is not 'behind the curtains' is of no concern
    > to the programmer.


    ok, no concern :)

    > Now, the expression (ivec.size() / 2) has type std::vector<int>::size_type.
    > The compiler is telling you that you cannot initialize an iterator from a
    > size_type. The compiler it correct: the interface of iterator does not
    > contain such a conversion (for good reasons!).


    thanks Kai.

    "arnuld"
    www.arnuld.blogspot.com
     
    yoge, Oct 12, 2006
    #3
  4. arnuld

    Ron Natalie Guest

    Kai-Uwe Bux wrote:
    > arnuld wrote:
    > std::vector<int>::iterator mid_iter = (ivec.size() / 2);
    >
    > The variable mid_iter has type std::vector<int>::iterator. Note that
    > std::vector<int>::iterator, according to the standard, is an implementation
    > defined type. What it is or is not 'behind the curtains' is of no concern
    > to the programmer.
    > =


    mid_iter = ivec.begin() + (ivec.size() / 2)

    would however do what the OP was trying to accomplish.
     
    Ron Natalie, Oct 12, 2006
    #4
  5. arnuld

    Kai-Uwe Bux Guest

    Ron Natalie wrote:

    > Kai-Uwe Bux wrote:
    >> arnuld wrote:
    >> std::vector<int>::iterator mid_iter = (ivec.size() / 2);
    >>
    >> The variable mid_iter has type std::vector<int>::iterator. Note that
    >> std::vector<int>::iterator, according to the standard, is an
    >> implementation defined type. What it is or is not 'behind the curtains'
    >> is of no concern to the programmer.
    >> =

    >
    > mid_iter = ivec.begin() + (ivec.size() / 2)
    >
    > would however do what the OP was trying to accomplish.


    True, but the OP already knew that [from the OP as quoted in my previous
    posting]:

    arnuld wrote:

    > i wrote a programme to create a vector of 5 elements (0 to 4), here is
    > the code & output:

    [snip]
    > // create a middle iterator
    > std::vector<int>::iterator mid_iter = (ivec.begin() + ivec.size()/2);

    [snip]
    > now if i change the "mid_iter" to:
    >
    > /std::vector<int>::iterator mid_iter = (ivec.size() / 2);/
    >
    > then i got an ERROR:



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 12, 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. Andreas Suurkuusk
    Replies:
    0
    Views:
    3,997
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,178
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,979
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    404
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    558
    Bullschmidt
    May 11, 2004
Loading...

Share This Page