473,215 Members | 1,242 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,215 software developers and data experts.

"vector<int>::iterator" problem

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
4 5637
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.