By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,504 Members | 1,724 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,504 IT Pros & Developers. It's quick & easy.

return value

P: n/a
i want to return an empty SET object,i write it as "return set<string>
temp",but there are some error when complie it,and i have told that i
should write it as "return set<stringtemp()",who can
tell me why?
thank you!
Nov 6 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
SG
On 6 Nov., 09:02, jerry <machel2...@yahoo.cnwrote:
i want to return an empty SET object,i write it as "return set<string>
temp",but there are some error when complie it,and i have told that i
should write it as "return set<stringtemp()",who can
tell me why?
thank you!
return set<string>(); // default constructed temporary

You should be aware of that returning containers that way may be
expensive because all the container's entries are copied. If you don't
want this try one of these alternatives:

* add a function parameter: a pointer or reference to a set where
the result should be stored into and make the function void.
* create the set on the heap, put the pointer into a std::auto_ptr,
and return the auto_ptr object (not very popular these days, I
think)
* wait for an implementation of the upcoming C++ standard
(move semantics)

Cheers,
SG
Nov 6 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.