473,800 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi,
I have some code like this,

vector<MyNode*g etNodes() {
return theNodes;
}

int func() {
vector<MyNode*n odes;
nodes.insert(no des.end(), getNodes().begi n(), 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*n odes, tmp_nodes;
tmp_nodes=getNo des();
nodes.insert(no des.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 2028
On 10 Sep 2006 20:55:36 -0700 in comp.lang.c++, li*****@hotmail .com
wrote,
vector<MyNode*g etNodes() {
return theNodes;
}

The above returns a copy of "theNodes" to the caller.
int func() {
vector<MyNode*n odes;
nodes.insert(no des.end(), getNodes().begi n(), 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*g etNodes() {
return theNodes;
}

int func() {
vector<MyNode*n odes;
nodes.insert(no des.end(), getNodes().begi n(), getNodes().end( ));

}
I suspect you want this:

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

void func() {
vector<MyNode*n odes;
nodes.insert(no des.end(), getNodes().begi n(), getNodes().end( ));
}

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

vector<MyNode*g etNodes() {
return theNodes;
}

int func() {
vector<MyNode*n odes;
nodes.insert(no des.end(), getNodes().begi n(), 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*n odes, tmp_nodes;
tmp_nodes=getNo des();
nodes.insert(no des.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
3538
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 to class Conception instead? How can I do that? And how should I write to declare an iterator to this vector?
2
2245
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
2
3328
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
7
12637
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 added at the front (index 0) of the vector. If the vector contains a certain amount of elements, the element at the back is removed when a new one is added. If one tries to add a string already stored in the vector, that string is supposed to...
3
2553
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 my implementation is not natural in the way that I do not supply the vector directly. How can I improve on that? (1) template <class T>
0
9691
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
10507
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
10279
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...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
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
7582
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
5473
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...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2948
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.