473,387 Members | 1,510 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,387 software developers and data experts.

Does containers get copied when used as parameters?

When I do something like:

void Foo(string s)
{
// something here
}

does ti copy S, or as with a char* it only copies the location where
the string is?

More generally when I use a container as parameter does it pays to pass
a reference as a parameter to avoid the container being copies?

Thanks!

Jul 2 '06 #1
4 1126
When I do something like:
>
void Foo(string s)
{
// something here
}

does ti copy S, or as with a char* it only copies the location where
the string is?
It copies s.
More generally when I use a container as parameter does it pays to pass
a reference as a parameter to avoid the container being copies?
Yes.

void Foo(string& s)
{
// something here
}

//eric
Jul 2 '06 #2
>More generally when I use a container as parameter does it pays to pass
>a reference as a parameter to avoid the container being copies?

Yes.
I forgot to say: that is if you're not going to change the value of the
parameter. However, in some cases where you change the value of the
parameter and the caller no longer will use the variable it still pays. But
i recommend that you will only pass a variable as a reference if you're not
changing it.

void foo (const std::string& s) {
// whatever
}

//eric
Jul 2 '06 #3

"Eric Jensen" <er**@no.spam.comwrote in message
news:44***********************@dread11.news.tele.d k...
>>More generally when I use a container as parameter does it pays to pass
a reference as a parameter to avoid the container being copies?

Yes.

I forgot to say: that is if you're not going to change the value of the
parameter. However, in some cases where you change the value of the
parameter and the caller no longer will use the variable it still pays.
But i recommend that you will only pass a variable as a reference if
you're not changing it.

void foo (const std::string& s) {
// whatever
}
Are you only referring to a const reference here? Passing by non-const
reference is a perfectly fine way to pass objects whose contents may be
modified.

-Howard
Jul 3 '06 #4
Are you only referring to a const reference here? Passing by non-const
reference is a perfectly fine way to pass objects whose contents may be
modified.
I nearly meant, if the argument are not to be changed, always pass as a
reference.
And if the caller expect that the argument is not to be manipulated, then do
not pass as a reference if the function modifies the object. And of course
you can still pass the argument as a reference, if the purpose of your
function is to modify the object for the caller.

//eric
Jul 3 '06 #5

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

Similar topics

18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
5
by: Tommo | last post by:
I am storing some key value pairs in a map as follows: <code> bool StaticDataCache::putMarket(uint8_t key,item_t* value) { typedef pair<uint8_t,item_t*> entry; typedef...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
11
by: jimxoch | last post by:
Hi list, Most STL containers are storing their data on the heap. (although some std::string implementations are notable exceptions) Of course, using the heap as storage increases flexibility and...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.