c++ - 如何存储对象供以后使用并使它们可搜索

标签 c++ class object pointers struct

目前,每次创建一个对象时,我都使用一个 vector 来存储指向该对象的指针,但这感觉有点傻。可能有更好的方法,但我还没有找到。

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
};

这是我目前存储它们的方式

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);
}

因此,为了检索给定日期的项目,我目前正在检查 vector 中的所有 指针,以查看“stackDate”属性是否与请求的日期匹配,如果是,我使用“thePaymentItem"属性以显示实际项目。

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 个问题:

  1. 遍历 vector 中的所有项目是非常低效的,如果我只 需要一些建议
  2. RecPaymentStack 目前实际上完全没用,因为我应该做的是,在添加新项目时,检查是否已经为新项目的“RecPaymentStack.stackDate”创建了Date 属性,如果是,则将指向“RecPayments”的新指针添加到“RecPaymentStack”对象内的指针数组。但是如何呢?
  3. 所有这些从一开始就感觉非常愚蠢。可能有更简单/更专业的方法来做到这一点,但我不知道是什么,可能是因为我仍然像 PHPer 一样思考。

所以这里的一般想法是我最终做了类似(愚蠢的例子)

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;
    }
}

这基本上会遍历所有唯一的“RecPaymentsStack”对象(唯一性由它的“日期”属性确定),然后对于每个日期,它会显示它是来自 RecPayments 结构的“子对象”。

并且必须有某种方法来搜索特定日期,而不必遍历所有可用日期。

最佳答案

与其使用 vector 来管理您的项目,不如将您的 RecPaymentsStack 实例替换为 std::multimap .键类型是您的 Date 结构,值类型是 RecPayments(我将其更改为单数形式 RecPayment)。小例子(未经测试):

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;
}

关于c++ - 如何存储对象供以后使用并使它们可搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379041/

相关文章:

c++ - 如何访问属于另一个类的私有(private)成员的类的方法

c++ - 我在哪里存储八叉树中的形状?

java - 在 Java 中创建对象时,如何将现有数字加 1?

c# - 将输入添加到数组的构造函数

c++ - 如何使存储在 vector 中的对象中的数据唯一?

Javascript 数组操作 - 是否有更具声明性的方法?

c++ - 在 C++ 中右对齐 getline() 输入

c++ - 'private'访问修饰符是否给了编译器更大的优化空间?

Java Class子类变量引用

c++ - 由定义指令定义的类名。可能的?