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 7592947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:02:20+00:00 2026-05-30T21:02:20+00:00

I need a container of containers like: class Widget { … }; … std::vector<std::list<Widget>

  • 0

I need a container of containers like:

class Widget {
  ...
};
...
std::vector<std::list<Widget> > widgets;

Because STL containers copy objects in them, and copy a container is not a cheap operation, I think this container of containers may leads to really bad efficiency and therefore a bad design.
I want to know if I am right. And if it is, should I use container of container pointers or something else?
Thanks

p.s.

Thanks for you guys, and now I know whether it’s a bad design or not depends on how I use it.

So if I know how many list(at most) would be inserted into vector and then use vector::reserve to avoid vectorreallocation and after that I just query about objects in it, this may be a good design.

On the other hand, if I need insert list into the vector every now and then, this may leads to really bad efficiency.

Am I right?

  • 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-30T21:02:21+00:00Added an answer on May 30, 2026 at 9:02 pm

    It is not bad design. It is the way you use it that can be bad design. If your code requires frequent deep copying and you implement std::vector<std::list<Widget> *> widgets, but still require a deep copy, there isn’t much difference with what you are doing.

    If for example, you are querying some member of the lists in the vector very frequently, this may actually be good design because of the memory proximity due to the compactness of the vector.

    Modern compilers with C++11 support that has move semantics usually move data instead of copying data. So I will not be too concerned about vector relocation or shifting. ‘Moving’ can still be done by older compilers using std::swap whenever it makes more sense than copying.

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

Sidebar

Related Questions

class X { public: typedef std::list<int> Container; // (1) const Container& GetElements() const; //
I need a template like this, which work perfectly template <typename container> void mySuperTempalte
I want to write a container class that acts like a dictionary (actually derives
class Equipment { std::vector<Armor*> vEquip; Weapon* mainWeapon; int totalDefense; int totalAttack; public: unsigned int
I have a need for a container that acts like the following. It has
I'm working on an container class that looks something like this: class hexFile {
I need an associative container that makes me index a certain object through a
I need a fast container with only two operations. Inserting keys on from a
I need a generic container that keeps its elements sorted and can be asked
I have a container element which I need to resize as its contents change.

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.