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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:21:07+00:00 2026-05-16T14:21:07+00:00

My question is best illustrated with a code sample, so let’s just start off

  • 0

My question is best illustrated with a code sample, so let’s just start off with that:

class Game
{
    // All this vector does is establish ownership over the Card objects
    // It is initialized with data when Game is created and then is never
    // changed.
    vector<shared_ptr<Card> > m_cards;

    // And then we have a bunch of pointers to the Cards.
    // All these pointers point to Cards from m_cards.
    // These could have been weak_ptrs, but at the moment, they aren't
    vector<Card*> m_ptrs;

    // Note: In my application, m_ptrs isn't there, instead there are
    // pointers all over the place (in objects that are stored in member
    // variables of Game.
    // Also, in my application, each Card in m_cards will have a pointer
    // in m_ptrs (or as I said, really just somewhere), while sometimes
    // there is more than one pointer to a Card.
}

Now what I want to do is to make a deep copy of this Game class. I make a new vector with new shared_ptrs in it, which point to new Card objects which are copies of the original Card objects. That part is easy.

Then the trouble starts, the pointers of m_ptrs should be updated to point to the cards in m_cards, which is no simple task.

The only way I could think of to do this is to create a map and fill it during the copying of m_cards (with map[oldPtr] = newPtr) and then to use that to update m_ptrs. However, this is only O(m * log(n)) (m = m_ptrs.size(); n = m_cards.size()). As this is going to be a pretty regular operation* I would like to do this efficiently, and I have the feeling that it should be possible in O(m) using custom pointers. However, I can’t seem to find an efficient way of doing this. Anybody who does?

*it’s used to create a testbed for the AI, letting it “try out” different moves


Edit: I would like to add a bit on accepting an answer, as I haven’t yet. I am waiting until I get back to this project (I got on a side track as I had worked too much on this project – if you do it for fun it’s got to stay fun), so it may be a while longer before I accept an answer. Nevertheless, I will accept an answer some time, so don’t worry 😛


Edit nr 2: I still haven’t gotten back to this project. Right now, I am thinking about just taking the O(m * log(n)) way and not complaining, then seeing later if it needs to be faster. However, as I have recently taken some time to learn my patterns, I am also thinking that I really need to refactor this project some time. Oh, and that I might just spend some time working on this problem with all the new knowledge I have under my belt. Since there isn’t an answer that says “just stick with the hashmap and see later if it really needs to be faster” (and I would actually be pretty disappointed if there was, as it’s not an answer to my question), I am postponing the picking of an answer yet a bit more till I do get back to this project.


Edit nr 3: I still didn’t get back to this project. More precisely, it has been shelved indefinitely. I am pretty sure I just wouldn’t get my head too bent over the O(m * log(n))right now, and then perhaps look at it later if it turned out to be a problem. However, that would just not have been a good answer to my question, as I explicitly asked for better performance. Not wanting to leave the answers unaccepted any longer, I chose the most helpful answer and accepted it.

  • 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-16T14:21:08+00:00Added an answer on May 16, 2026 at 2:21 pm

    Store the pointers as indexes.
    As you say they all point to m_Cards which is a vector that can be indexed (is that correct English?).
    Either you do that only for storing and convert them back to pointers at loading.
    Or you may think of using indices generally instead of pointers.

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

Sidebar

Related Questions

Just a quick question about best practice in MVC development. Let's say that I've
Best way to illustrate my question is with this example code: class Item {}
This is a question best illustrated by example: User goes to Site A ,
Let me illustrate this question with code first: with Ada.Text_IO; procedure Test is task
This question ( Best way to strip punctuation from a string in Python )
Simple question on best practice. Say I have: public class Product { public string
I have a question about best practices in Java. I'm writing a class, which
My question is best illustrated with an example. Suppose I have the enum: public
Ok I nabbed method C for preloading images from this question best way to
This question has to do with best practices... please do not confuse it with

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.