473,466 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

const vs non-const version of begin( )

Suppose I have

vector<intv;
vector<int>::const_iterator citer = v.begin( );

Here which version of begin( ) is called ? const or non-const
version ?

Kindly explain

Thanks
V.Subramanian

Oct 25 '07 #1
3 2409
su**************@yahoo.com, India wrote:
Suppose I have

vector<intv;
vector<int>::const_iterator citer = v.begin( );

Here which version of begin( ) is called ? const or non-const
version ?
Should be non-const ...

Kindly explain
.... because v is not const.
Best

Kai-Uwe Bux
Oct 25 '07 #2
On 2007-10-25 09:59, su**************@yahoo.com, India wrote:
Suppose I have

vector<intv;
vector<int>::const_iterator citer = v.begin( );

Here which version of begin( ) is called ? const or non-const
version ?
The non-const version. The reason for this is the fact that v is not
const. Consider the following example:

#include <vector>

void foo(const std::vector<int>& v)
{
std::vector<int>::const_iterator it = v.begin();
}

int main()
{
std::vector<intv;
std::vector<int>::iterator it = v.begin();
std::vector<int>::const_iterator cit = v.begin();

foo(v);
}

In foo() you must use a const_iterator since v is const, in main()
however you can use either since a const_iterator can be constructed
from an iterator. Notice though that I have not found anything in the
standard requiring this, so it might not be portable.

--
Erik Wikström
Oct 25 '07 #3
On 2007-10-25 03:59:36 -0400, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:
Suppose I have

vector<intv;
vector<int>::const_iterator citer = v.begin( );

Here which version of begin( ) is called ? const or non-const
version ?
As everyone else has said, non-const. If that's a problem, there are
two ways to call the const version.

((const vector<int>&)v).begin();
v.cbegin();

The latter is a recent addition to the C++0x specification, so chances
are your library doesn't have it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
1
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...
0
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,...
0
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...
0
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...
0
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 ...

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.