return value

J

jerry

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<string> temp()",who can
tell me why?
thank you!
 
S

SG

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<string> temp()",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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top