Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:35:46+00:00 2026-05-22T01:35:46+00:00

What is the best way (performance-wise) of returning stl containers from a function? The

  • 0

What is the best way (performance-wise) of returning stl containers from a function? The container returned would usually contain several thousands of items.

Method 1:

typedef std::list<Item> ItemContainer;

ItemContainer CreateManyItems() {
    ItemContainer result;

    // fill the 'result' ...

    return result;
}

ItemContainer a = CreateManyItems();

Method 2:

void CreateManyItems(ItemContainer &output) {
    ItemContainer result;

    // fill the 'result' ...

    output.swap(result);
} 

ItemContainer a;
CreateManyItems(a);

Method 3:

void std::auto_ptr<ItemContainer> CreateManyItems() {
    std::auto_ptr<ItemContainer> result(new ItemContainer);

    // fill the 'result' ...

    return result;
}

std::auto_ptr<ItemContainer> a = CreateManyItems();

Or is there any better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:35:47+00:00Added an answer on May 22, 2026 at 1:35 am

    None: if you just want to fill std::list with items, then you can use std::fill or std::fill_n or a combination of standard library functions.

    It’s not clear how exactly you want to fill your list, so I can’t comment on your code precisely. If possible, use the standard library. If you cannot, then go for Method 1, and the compiler may optimize away the return value in your code eliding the unnecessary copies, as most compilers implement RVO.

    See these articles on copy elision and return value optimization (RVO):

    • Copy elision – Wikipedia
    • Copy elision – cppreference

    Related questions:

    • In C++, is it still bad practice to return a vector from a function?
    • Returning a c++ std::vector without a copy?

    An article by Dave Abrahams:

    • Want Speed? Pass by Value

    I would still emphasize this: have you seen all the generic functions provided by <algorithm> header? If not, then I would suggest you to first look into them and see if any of them (or a combination of them) can do what you want to do in your code.

    If you want to create and fill the list, then you can use std::generate() or std::generate_n function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what the best way (performance wise) to access a
What is the best way (performance wise) to paginate results in SQL Server 2000,
If there's a TextBlock inside a Grid, what's the best way (performance wise) to
What is the best way performance wise to take a list of words and
What is best way to write query performance wise. e.g. i can write query
Maybe that's a silly question... But what's the best (performance and memory wise) way
Which is the best way (in performance and security) to send multiple parameters to
I'm trying to find out the most efficient (best performance) way to check date
What is the best (regarding performance) way to compute the critical path of a
I'm currently trying to find the best way (in term of usability and performance)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.