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

  • Home
  • SEARCH
  • 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 6900935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:36:37+00:00 2026-05-27T07:36:37+00:00

At the moment I am using a vector to store pointers to the object

  • 0

At the moment I am using a vector to store pointers to the object every time one is made, but that feels a little silly. There’s probably a better way, but I haven’t found it.

What I'm doing:                      Example usage:

prototype

The problem:

  1. If I want to retrieve a certain Date I have to go over all items in the vector to see if RecPaymentsStack.stackDate matches the date the user requested.
  2. The RecPaymentStack is actually completely useless at the moment because what I should be doing, is, when adding a new item, checking if a “RecPaymentStack.stackDate” has already been made for the new item’s Date property, and if so add the new pointer to “RecPayments” to an array of pointers inside the “RecPaymentStack” object. But how?

I’m probably unnecessarily complicating things (something I do a lot) so an explenation on how something like this should be done would be very nice.

Detailed info: (in case I was being too vague)

The below example is supposed to resemble a calendar that can hold certain items (RecPayments) and those items are grouped by their date (RecPaymentsStack).

struct RecPayments
{
    std::string name;
    Date* date;
    float cost;
};

struct RecPaymentsStack
{
    Date* stackDate; //This stack's date
    RecPayments * thePaymentItem; //Hold pointer to the actual item
};

And here’s how I’m currently storing them

std::vector<RecPaymentsStack*> RecPaymentsVector; //This vector will hold pointers to all the Recurring Payments

void addRecurring(std::string theDate,std::string theName,float theCost)
{
    //New recurring payment
    RecPayments * newPaymentItem = new RecPayments;
    //Set recurring payment properties
    newPaymentItem->name = theName;
    newPaymentItem->date = new Date(stringToChar(theDate));
    newPaymentItem->cost = theCost;

    //Add recurring payment to stack
    RecPaymentsStack * addToStack = new RecPaymentsStack;
    addToStack->stackDate = new Date(stringToChar(theDate));
    addToStack->thePaymentItem = newPaymentItem;

    //Add pointer to RecPaymentsStack to vector
    RecPaymentsVector.push_back(addToStack);
}

So to retrieve the items for a given date, I am currently going over all pointers in the vector to see if the “stackDate” property matches the requested date, and if so I use the “thePaymentItem” property to show the actual item.

void getItemsNow(Date requestedDate)
{
    std::cout << "Showing Dates for " << requestedDate << std::endl;
    unsigned int i;
    for(i=0;i<RecPaymentsVector.size();i++) //Go over all items in vector
    {
        Date dateInVector(*RecPaymentsVector[i]->stackDate); //Get the date from the vector
        if(dateInVector == requestedDate) //See if Date matches what the user requested
        {
            //Date matched, show user the item properties.
            std::cout << "Date: " << dateInVector <<
                " has name: " << RecPaymentsVector[i]->thePaymentItem->name <<
                " and price " << RecPaymentsVector[i]->thePaymentItem->cost <<
                std::endl;
        }
    }
}

3 problems with this:

  1. Going over all items in the vector is highly inefficient if I only
    need a couple of pointers
  2. The RecPaymentStack is actually completely useless at the moment because what I should be doing, is, when adding a new item, checking if a “RecPaymentStack.stackDate” has already been made for the new item’s Date property, and if so add the new pointer to “RecPayments” to an array of pointers inside the “RecPaymentStack” object. But how?
  3. All of this feels extremely silly to begin with.. there’s probably a much easier/professional way to do this but I can’t find out what, probably because I’m still thinking like a PHPer.

So the general idea here is that I end up doing something like (silly example)

for each RecPaymentsStack->stackDate //For each unique Date, show it's children items.
{
    cout << "The Date is " CurrentRecPaymentsStack->stackDate and it holds the following items:
    for each CurrentRecPaymentsStack->thePaymentItem //This would now be an array of pointers
    {
        cout << "item name " CurrentRecPaymentsStack->thePaymentItem->name << " with cost " << CurrentRecPaymentsStack->thePaymentItem->cost << endl;
    }
}

Which would basically go over all the unique “RecPaymentsStack” objects (unique determined by it’s “Date” property) and for each Date it would then show it’s “children” from the RecPayments struct.

And there has to be some way to search for a particular date without having to go over all the available ones.

  • 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-27T07:36:38+00:00Added an answer on May 27, 2026 at 7:36 am

    Rather than using a vector to manage your items, you should replace your RecPaymentsStack instance with a std::multimap. The key type is your Date structure, the value type is RecPayments (which I would change to the singular form RecPayment). Small example (untested):

    typedef std::multimap<Date, RecPayment> RecPaymentsByDateMap;
    typedef std::pair<RecPaymentsByDateMap::iterator, 
                      RecPaymentsByDateMap::iterator>
                                            RecPaymentsByDateMapIters;
    
    RecPaymentsByDateMap payments_by_date;
    
    RecPaymentsByDateMapIters findByDate(Date date) {
      return payments_by_date.equal_range(date);
    }
    
    ...
    
    // find all payments with the given date
    RecPaymentsByDateMapIters iters = findByDate(...);
    for (RecPaymentsByDateMap::iterator it = iters.first;
         it != iters.second;
         ++it)
    {
      std::cout << "Payment " << it->second.name << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java client that calls a web service at the moment using
I am making a little GUI frontend for a app at the moment using
I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
I'm writing a program that uses OOP to store student records. At the moment
I'm writing a MUD (text based game) at the moment using java. One of
At the moment am using JOGL for a ball detection program I have been
At the moment I'm using JavaFX only for fun and learning it. What I
I have a self built JSP webapp and at the moment I'm using tomcats
I am using Oracle 10g at the moment. I want to search through all
I am using in my code at the moment a ReentrantReadWriteLock to synchronize access

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.