473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"vector<int>::i terator" 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<int ivec;

// dynamically create a vector
for(std::vector <int>::size_typ e ix = 0; ix <= 4; ++ix)
{
ivec.push_back( ix);
}
// print-out the elements
for(std::vector <int>::iterat or 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::__no rmal_iterator<i nt*, 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 5673
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_typ e ix = 0; ix <= 4; ++ix)
{
ivec.push_back( ix);
}
// print-out the elements
for(std::vector <int>::iterat or 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::__no rmal_iterator<i nt*, 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<in t>::iterator, according to the standard, is an
implementati on 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
2119
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 we have an integer list p, say. For each element x in p, we want to repace x with f(x) to get a new, possibly larger, integer list. Note
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.