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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:05:06+00:00 2026-06-13T07:05:06+00:00

I want to insert new element into a vector using a pointer I have

  • 0

I want to insert new element into a vector using a pointer I have the following sample code:

struct info {
    string Name;
    int places;  // i will use the binary value to identfy the visited places example 29 is 100101
                 // this means he visited three places (London,LA,Rome)  
    vector<int> times; // will represent the visiting time,e.g. 1,2,5 means london 1 time, LA
                       // twice and Rome five times
};

map<string,vector<info> *> log;

Peaple are coming from different cities, I will check if the city exists, just add the new person to the vector, else creat a new map object:

vector<info> tp;
info tmp;
if(log.size()==0|| log.count(city)==0) //empty or not exist 
{
    tp.push_back(tmp);
    vector<info>* ss = new vector<info>;
    ss=&(tp);
    // create a new object 
    log.insert(map<string,vector<info> * >::value_type(city,ss)); // new object 
}
else // city exist, just add the information to the vector
{
    map<string,vector<info> *>::iterator t;
    t=log.find(city);
    *(t->second).push_back(tmp);  //the problem  in this line
}

How can I insert the new tmp into the vector?

The information to be read as follows:

Paris,Juli,5,3,6
Paris,John,24,2
Canberra,John,4,3
London,Mary,29,4,1,2
  • 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-13T07:05:07+00:00Added an answer on June 13, 2026 at 7:05 am

    There are a lot of mistakes here, and they all stem from misusing pointers. The line that is mentioned as the cause of the problem is a minor syntactic issue. There are bigger issues at hand.

    All of them can be easily solved by not misusing pointers. There is no reason to use pointers here, so the ultimate fix is to make the map have this type map<string,vector<info>> log;.

    Then the code becomes something like this:

    info tmp;
    log[city].push_back(tmp);
    // the [] operator creates a new empty vector if it doesn't exist yet
    // there's no point in doing the checks by hand
    

    Now that we have a simple solution, I’ll mention the elephant in the room code.

    vector<info>* ss = new vector<info>;
    ss=&(tp);
    // ...
    log.insert(map<string,vector<info> * >::value_type(city,ss));
    

    This sequence of operations will create a vector with dynamic storage duration, and immediately discard the sole pointer to it. That causes the vector that was just created to be lost, and the memory it uses is leaked; it cannot be recovered anymore.

    To make matters worse, it sets ss to point to a local variable, and then saves that pointer to the local variable in the map. Because the local variable has automatic storage duration, it is gone once the function returns. That makes the pointer that was just stored in the map invalid, because it no longer has a vector to point to. After that, all kinds of havoc would be wreaked.

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

Sidebar

Related Questions

I want to insert a pair< string, vector<float> > into a map, first it
I am using the html table. I want to insert a new row to
I'm new to Java. I want to create Java Array and insert data into
I want insert a custom snippet into an XML file from code within an
I want insert new a element in panel, the insert is good but hide.
I'm writing a little piece of code where I'll have to insert values into
What is the best way to insert a new li element into a specific
Consider the following code: var myDict = new Dictionary<string, int>(); myDict.Add(Key1, 1); myDict.Add(Key2, 2);
I want some help for someone about how to insert a new element in
I have tried using the inject, adopt and grab methods to insert my new

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.