"vector<int>::iterator" problem

A

arnuld

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"
 
K

Kai-Uwe Bux

arnuld said:
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
 
Y

yoge

Kai-Uwe Bux said:
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
 
R

Ron Natalie

Kai-Uwe Bux said:
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.
 
K

Kai-Uwe Bux

Ron said:
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]:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top