473,406 Members | 2,390 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,406 software developers and data experts.

STL vector : can not use begin() and end() on function return

Hi,
I have some code like this,

vector<MyNode*getNodes() {
return theNodes;
}

int func() {
vector<MyNode*nodes;
nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());

}
This code crashes on Windows using VC7 compiler. In debuging it I
find that I have to change the code to:

int func() {
vector<MyNode*nodes, tmp_nodes;
tmp_nodes=getNodes();
nodes.insert(nodes.end(), tmp_nodes.begin(), tmp_nodes.end());
}
It looks like I can use begin() and end() directly to a function
return, but I am wondering why?

Sep 11 '06 #1
3 2010
On 10 Sep 2006 20:55:36 -0700 in comp.lang.c++, li*****@hotmail.com
wrote,
vector<MyNode*getNodes() {
return theNodes;
}

The above returns a copy of "theNodes" to the caller.
int func() {
vector<MyNode*nodes;
nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());

}
The above calls getNodes twice, getting two copies of theNodes. It
then tries to compare the .begin() of one of them to the .end() of
the other one. They are not comparable since they belong to two
different vectors. Boom.

Sep 11 '06 #2

li*****@hotmail.com wrote:
Hi,
I have some code like this,

vector<MyNode*getNodes() {
return theNodes;
}

int func() {
vector<MyNode*nodes;
nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());

}
I suspect you want this:

vector<MyNode*const & getNodes() { // nb return type
return theNodes;
}

void func() {
vector<MyNode*nodes;
nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());
}

Sep 11 '06 #3
li*****@hotmail.com wrote:
Hi,
I have some code like this,

vector<MyNode*getNodes() {
return theNodes;
}

int func() {
vector<MyNode*nodes;
nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());

}
This code crashes on Windows using VC7 compiler. In debuging it I
find that I have to change the code to:

int func() {
vector<MyNode*nodes, tmp_nodes;
tmp_nodes=getNodes();
nodes.insert(nodes.end(), tmp_nodes.begin(), tmp_nodes.end());
}
It looks like I can use begin() and end() directly to a function
return, but I am wondering why?

Each time you call getNodes() in that last line of func(), you get a new
copy of theNodes.

You basically have two choices:

1) Use a temporary variable as you do above.
2) Change getNodes to return a reference, like so:
const vector<MyNode*&getNodes() {
return theNodes;
}

#2 has the advantage that it doesn't cause a new copy of theNodes to be
made.

--
Clark S. Cox III
cl*******@gmail.com
Sep 11 '06 #4

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

Similar topics

14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
7
by: William Payne | last post by:
(This post is related to "recent files menu"-post below. If I should have kept this in that thread, I apologise.) Hello, I have a function that adds a std::string to a std::vector. New entries are...
3
by: utab | last post by:
Dear all, I have the below template (1) which I would like to implement to accept either a vector or an array. I thought on it some time and came with a solution as in (2) , however the call of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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...
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
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...

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.