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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:03:26+00:00 2026-06-18T13:03:26+00:00

In my C++ application, I have Pod objects that internally stores information about Monkey

  • 0

In my C++ application, I have Pod objects that internally stores information about Monkeys in a vector. MonkeyInternal is a big class with a lot of properties about monkeys…

My intention here is to create a lightweight proxy object Monkey that merely stores an index that can be used to access more information stored in a private vector in Pod (Monkeys are capricious, and their properties may change, and we want any monkey objects in the program to always be up to date.).

class Pod {
  public:
    typedef Monkey monkey_type;

    Monkey monkey(int monkey_index) {
      return Monkey(this, monkey_index);
    }

    // Lightweight proxy Monkey object that can be passed around easily.
    class Monkey {
      public:
        Monkey(Pod* pod, int monkey_index) {
          pod_ = pod;
          monkey_index_ = monkey_index;
        }
      private:
        Pod* pod_;
        int monkey_index_; // To get monkey internal info, index into pod.
        friend class Pod; // Monkey is a proxy that delegates to Pod.
    }

  private:
    // MonkeyInternal objects stores hefty data about monkeys.
    vector<MonkeyInternal> monkeys_;
}

With these data structures, I can randomly access a monkey’s internal information easily. I can also easily insert another monkey into the pod efficiently (O(1) amortized).

However, removing a monkey is O(n) since I must shift all the monkeys to the left. I could make removal slightly more efficient by using an unordered_map (or some good hash table) – O(n), but is there any way I could organize my data structures to support O(1) removal of a monkey?

Perhaps I could use a clever design pattern?

  • 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-06-18T13:03:27+00:00Added an answer on June 18, 2026 at 1:03 pm

    Your Monkey class is effectively a smart pointer — it might be nice to make it behave like one syntactically as well so that users have the right mental model for what they’re dealing with.

    As for the removal, I assume that you’re removing the MonkeyInternal that is stored by value in the vector? If the copying a MonkeyInternal is expensive then the best way to save is to store something cheap in the vector, like a MonkeyInternal* instead.

    Once you go down this route you may well find it better to replace your Monkey type with something like std::shared_ptr and just be done with it.

    BTW, so long as the copy is cheap, linear memory access is pretty cheap as the CPU memory prediction can work out what’s going on really well. This falls apart if you have to chase pointers so you’ll probably find that std::vector is faster than std::list even though it is technically O(n) rather than O(1) for insert and removal.

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

Sidebar

Related Questions

Should a web application have a notices/about page that details the components/libraries that we
In my console application have an abstract Factory class Listener which contains code for
In My application have time consuming process.There fore i try to do that operation
I have an android application that binds itself to a remote service once the
I have PHP application that contain three small applications. Each application have own users
My application have a configuration xml-file. That file contains more than 50 program settings.
I have a Delphi application that communicates with web servers on the Internet using
The tests that I have written for my Django application have been working perfectly
My problem is that my application have around 12-13 screen, and all of that
Hi! I have a trouble with duplicating PendingIntents. My Application have Service that starts

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.