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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:59:43+00:00 2026-06-03T06:59:43+00:00

I’ve got a vector of std::string that represents the structure of another vector of

  • 0

I’ve got a vector of std::string that represents the structure of another vector of object pointers. I would like to generate this vector of object pointers, where if there are two of the same objects, the pointer in the vector is also the same.

Don’t know what I meant just there? let me give you an example. Let’s say I have a std::vector of std::string:

std::vector<std::string> names;
names.push_back(std::string("A"));
names.push_back(std::string("A"));
names.push_back(std::string("B"));
names.push_back(std::string("C"));
names.push_back(std::string("A"));

Now let’s say these names (A, B and C) represent three different instances of an Object:

Object* ptr_A = new Object(1); // A
Object* ptr_B = new Object(2); // B
Object* ptr_C = new Object(3); // C

I would now like to generate the vector that holds pointers to the Object, in the same order as in the vector names:

std::vector<Object*> objects;
objects.push_back(ptr_A);
objects.push_back(ptr_A);
objects.push_back(ptr_B);
objects.push_back(ptr_C);
objects.push_back(ptr_A);

so that when two names are the same, the pointers are the same also.

How would I efficiently do this? Note that I would like the objects to be dynamically allocated inside the function that does this, so that the vector can be used later on.

I will be using this to read the objects from a file based on a name, but I want to prevent making multiple objects that are basically the same.

Since this is homework I cannot use C++11 yet.

  • 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-03T06:59:44+00:00Added an answer on June 3, 2026 at 6:59 am

    This generates a lookup object that maps the names to allocated objects, and then fills the vector objects with the values based off the values in names. Simple.

    typedef std::map<std::string, Object*>::iterator iterator;
    typedef std::pair<std::string, Object*> value_type;
    std::map<std::string, Object*> lookup;  //or maybe unordered_map
    lookup.insert(value_type("A", ptr_A));
    lookup.insert(value_type("B", ptr_B));
    lookup.insert(value_type("C", ptr_C));
    
    for(int i=0; i<names.size(); ++i) {
        iterator iter = lookup.find(names[i]);
        if (iter == lookup.end())
             throw std::runtime_error("invalid name in file");
        objects.push_back(iter->second);
    }
    

    If you want to generate the names and objects from the file, you can do something like this to create the names, objects, and the mapping between them all at once. I assume that you will have to do other stuff as well, I don’t know your file format.

    std::string newname;
    std::map<std::string, Object*> lookup;  //or maybe unordered_map
    while(myfile >> newname)
        lookup[newname] = new Object(newname);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I would like to run a str_replace or preg_replace which looks for certain words
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but

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.