473,748 Members | 9,416 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 5704
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
2129
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
8984
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.