473,698 Members | 2,932 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 5699
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
2126
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
8683
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
9031
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
8904
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7741
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
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
5867
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
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
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
2007
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.