473,809 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning vector

what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.
Oct 26 '05 #1
8 1750
Richard wrote:
what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.

vector<int> f()
{
vector<int> xxx;

return xxx;
}

(rather an expensive operation if the vector is big however!)
Oct 26 '05 #2
Richard wrote:
what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.


Presumably you mean something like this:

vector<int> GetVec()
{
vector<int> v;
// Do something here to fill the vector
return v;
}

The problem is that this function returns by value, which means the
vector will likely be copied in its entirety, which could get expensive
unless your compiler can figure out that it doesn't need to copy it
(but I wouldn't depend on that). Alternately, you could pass the vector
in by reference:

void GetVec2( vector<int>& v )
{
// You might need to check to see if v holds anthing on entry
// (existing elements might be fine or might not be)

// Do something here to fill/change the vector
}

Now, no copying is involved no matter what optimizations you use.

There are some other techniques, too, but this latter one is probably
what you want.

Cheers! --M

Oct 26 '05 #3
Richard wrote:
what is the syntax for returning a vector?
Returning where? From where?
temp is a vector
of what? What do you mean by "temp is a vector".
return temp; ?
Most likely.
return temp<>; ?
This is very likely a syntax error.
return temp<int>; ?
No, but you may be getting close if 'temp' is a template-id.
I got a book that does not talk about how to do this.
Get a different book.
Any helps are
appreciated.


Read FAQ 5.8 while you're at it.

V
Oct 26 '05 #4
> vector<int> GetVec()
{
vector<int> v;
// Do something here to fill the vector
return v;
}

The problem is that this function returns by value, which means the
vector will likely be copied in its entirety, which could get expensive
unless your compiler can figure out that it doesn't need to copy it
(but I wouldn't depend on that). Alternately, you could pass the vector
in by reference:


Or wait for r-value references or use NTL now:

http://upp.sourceforge .net/srcdoc$pick_$en-us.html

Mirek
Oct 26 '05 #5
Gianni Mariani wrote:
vector<int> f()
{
vector<int> xxx;

return xxx;
}

(rather an expensive operation if the vector is big however!)


Why is that? Couldn't the vector data could be shared by reference
between the two copies of the vector, subject to copy-on-write?

Unix-like operating systems have a fork() function which this for an
entire address space.

It's perfectly good style to return strings from functions in C++, and
strings are essentially vectors of characters.

Oct 26 '05 #6
Kaz Kylheku wrote:
Gianni Mariani wrote:
vector<int> f()
{
vector<int> xxx;

return xxx;
}

(rather an expensive operation if the vector is big however!)

Why is that? Couldn't the vector data could be shared by reference
between the two copies of the vector, subject to copy-on-write?


It could not. operator[] overloading mechanism is not robust enough for
that:

Part of std::vector definition is that you are get non-const reference for

xxx[index]

now consider

vector<int> a;
vector<int> b;
.....
T& x = a[index];
b = a;
x = 123;

- there is no way how to detect last assignment and perform copy ("on
write"). You can to some degree solve this problem with operator[]
returning some sort of proxy instead of reference, but such solution
would render vector less usable...

Mirek
Oct 26 '05 #7
Richard wrote:
what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.


return temp of course.

Your book most likely doesn't talk about because the expert who wrote it
didn't realise that newbies often think things have to be different,
i.e. there must be some special syntax for vectors of something. Temp is
a variable, it matters not one bit that it is a vector, string, pointer,
or anything else, to return the value contained in a variable is always
the same syntax.

john
Oct 26 '05 #8
Gianni Mariani wrote:
vector<int> f()
{
vector<int> xxx;
return xxx;
}
(rather an expensive operation if the vector is big however!)


Only if your compiler does not do NRVO. The standard allows optimising the copy away.
Unfortunately only a few compilers do this, and because of this in a general case I prefer

template<class OutIt>
void f(OutIt out)
{
// (for example std::copy(begin , end, out))
}

std::vector<int > v;
f(std::back_ins erter(v));

This will always avoid the extra copy of the vector, no matter how good your compiler is.

--

Valentin Samko - http://www.valentinsamko.com
Oct 29 '05 #9

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

Similar topics

2
29523
by: tornado | last post by:
hi all, i am pretty new to c++. i have this problem for which i am unable to think a solution. i don't understand how to pass a vector refernce back to the callin function. And how this reference will be handled by the calling function ? Can any one in the group point me to the correct solution for it ? Any code snippets will be of great help. Thanks in advance.
9
11919
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is up to which size m would you expect vector<double> returns_by_value() {
18
2147
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
6
2304
by: Affan Syed | last post by:
Hi, first let me apologize before hand for not knowing this trivial issue and my inability to find it after searching on the web for an answer to what seems to be starightforward. I have used vectors (STL) for some time but have never had the need to actually return one from a function. what i mean is that does something like the code below work? (this is very artificial case but just consider that this is imp to what i want to do and...
8
2091
by: Derek | last post by:
Some authors advocate returning const objects: const Point operator+(const Point&, const Point&); ^^^^^ Returning a const object prevents some bad code from compiling: Point a, b, c; (a + b) = c; // error
13
2325
by: Gernot Frisch | last post by:
Which method is the fastest/best: std::vector<intfoo1() { std::vector<intv; ... reutun v; } std::vector<int>& foo2()
3
8815
by: Michele | last post by:
Hello: What is the syntax for returning a reference to a vector from a function? I have a private vector and I want to return it using a public get function (like in the code below), but I believe that what I have will make a copy of the vector, and my real vector is several hundred bytes. Do I have to cast it as a constant? What is the syntax for that (or where does one look)? Sorry if these are basic questions - alas, I've been...
7
2415
by: arnuld | last post by:
/* C++ Primer - 4/e * * 1st example from section 7.2.2, page 234 * returning 2 values from a function * * STATEMENT: * to find a specific value in a vector and number of times * that value occurs in th vector. */
2
1630
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
Following function void mdelr(int *ar1, int a, int b, int d ) { int i,j,tmp; int *temp; for (i=0; i<a; i++) {
0
9603
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,...
1
10391
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
10121
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...
1
7664
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
6881
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
5550
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
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
3015
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.