By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

"vector<int>::iterator" problem

P: n/a
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<intivec;

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

Oct 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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<intivec;

// 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
Oct 12 '06 #2

P: n/a
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

Oct 12 '06 #3

P: n/a
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.
Oct 12 '06 #4

P: n/a
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
Oct 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.