0
class Seller
{
private:
   float salestotal;     // run total of sales in dollars
   int lapTopSold;       // running total of lap top computers sold
   int deskTopSold;      // running total of desk top computers sold
   int tabletSold;       // running total of tablet computers sold
   string name;          // name of the seller
Seller::Seller(string newname)
{
   name = newname;
   salestotal = 0.0;
   lapTopSold = 0;
   deskTopSold = 0;
   tabletSold = 0;
}

bool Seller::SellerHasName ( string nameToSearch )
{
   if(name == nameToSearch)
      return true;
   else
      return false;
}
class SellerList
{
private:
   int num;  // current number of salespeople in the list
   Seller salespeople[MAX_SELLERS];
public:
   // default constructor to make an empty list 
   SellerList()
   {
      num = 0;
   }
   // member functions 

// If a salesperson with thisname is in the SellerList, this 
// function returns the associated index; otherwise, return NOT_FOUND. 
// Params: in
int Find ( string thisName );

void Add(string sellerName);

void Output(string sellerName);
};

int SellerList::Find(string thisName)
{
   for(int i = 0; i < MAX_SELLERS; i++)
      if(salespeople[i].SellerHasName(thisName))
         return i;
   return NOT_FOUND;
}

// Add a salesperson to the salespeople list IF the list is not full
// and if the list doesn't already contain the same name. 
void SellerList::Add(string sellerName)
{           
   Seller(sellerName);
   num++;
}

我的 SellerList 类中的函数中的参数存在一些问题。我想将某人添加到salespeople 数组中,这样我就有了所有卖家的记录... Bob、Pam、Tim 等... 我的构造函数Seller(sellerName) 创建了一个名为sellerName 的Seller。

如何将此卖方添加到 Salespeople 数组并有能力将数据拉回并用于更多功能,例如更新功能或输出功能?

MAX_SELLERS = 10 .... 我想我的问题是不知道是只使用 Add(string) 还是 Add(Seller, string) 的参数。任何帮助,将不胜感激。

4

3 回答 3

2

不是推倒重来。选择适合您问题的容器。在这种情况下,因为您通过 a 引用/搜索Sellers std::string,我建议您使用类似的哈希表std::unordered_map(或者std::map如果您无权访问 C++11,则使用搜索树):

int main()
{
    std::unordered_map<Seller> sellers;

    //Add example:
    sellers["seller name string here"] = /* put a seller here */;

    //Search example:
    std::unordered_map<Seller>::iterator it_result = sellers.find( "seller name string here" );

    if( it_result != std::end( sellers ) )
        std::cout << "Seller found!" << std::endl;
    else
        std::cout << "Seller not found :(" << std::endl;
}
于 2013-11-11T22:05:06.433 回答
0

如何在 SellerList 中使用 STD 向量而不是数组。

vector<Seller> x;

你可以做x.push_back(Seller(...))或者x[0].SellerHasName()并且x.size()会给你卖家的数量。

于 2013-11-11T20:53:49.300 回答
-1

也许是这样的?

// Add a salesperson to the salespeople list IF the list is not full
// and if the list doesn't already contain the same name. 
void SellerList::Add(string sellerName)
{           
   if(num < MAX_SELLERS)
       salespeople[num++] = new Seller(sellerName);
}
于 2013-11-11T20:53:01.847 回答