【问题标题】:How do I add vectors into my structs to create an inventory system in which I can add multiple different wines to the system using only one struct?如何将向量添加到我的结构中以创建一个库存系统,在该系统中我可以仅使用一个结构将多种不同的葡萄酒添加到系统中?
【发布时间】:2019-07-18 11:34:02
【问题描述】:

我有一个学校作业,我必须在其中创建一个葡萄酒库存系统,用户可以在其中添加多种不同的葡萄酒,我假设没有有限的数量。

我需要创建向量,但我不确定。我不知道该尝试什么。

#include <string>
#include <iostream>
#include <vector>
using namespace std;

struct Wine1
{   //struct for Wine pssibly needs Vector
    string name;
    string year;
    string place;
    string price;
} wine;
void printwine(Wine1 wine);


int main()
{
    string str; //input for data
    cout << "Please enter the data of the First wine: " << endl;
    cout << "Enter name: ";
    getline(cin, wine.name);
    cout << endl << "Enter year: ";
    getline(cin, wine.year);
    cout << endl << "enter country of creation: ";
    getline(cin, wine.place);
    cout << endl << "enter price: ";
    getline(cin, wine.price);
    cout << endl;

    cout << "your entered data: " << endl;
    printwine(wine);
    cout << endl;
    printwine2(wine2);
    cout << endl;
    printwine3(wine3);
}
void printwine(Wine1 wine)
{ //data the user typed as output
    cout << "Wine1" << endl;
    cout << "the name is: " << wine.name << endl;
    cout << "it's year is: " << wine.year << endl;;
    cout << "its country of creation is: " << wine.place << endl;;
    cout << "it's price is: " << wine.price << endl;
}

它应该为添加的每种葡萄酒输出葡萄酒的名称、年份、国家和价格。

【问题讨论】:

  • std::vector&lt;struct Wine1&gt; 可能是您需要的。
  • 对不起,如果这是一个愚蠢的问题,必须把这个放在哪里?

标签: c++ class c++11 data-structures stdvector


【解决方案1】:

一个好的开始应该是使用Wine1向量

std::vector<Wine1> wineVec;
wineVec.reserve(/*size*/) // reserve the memory if you know the number of wines beforehand

printwine 函数现在应该采用std::vector&lt;Wine1&gt;(最好是const-reference,因为数据是只读的)并遍历向量以打印Wine1 的属性。

类似:

#include <string>
#include <iostream>
#include <vector>

void printwine(const std::vector<Wine1>& vecWine)
{ 
    for (const auto& wine : vecWine)
    {
        // do printing each: wine.name, wine.year,... so on
    }
}


int main()
{
    std::vector<Wine1> vecWine;
    int wineNumber = 2;
    vecWine.reserve(wineNumber);

    std::string name, year, place, price;
    for (int i = 0; i < wineNumber; ++i)
    {
        // get the user input for name, year, place, and price
        std::cin >> name >> year >> place >> price;
        vecWine.emplace_back(Wine1{ name, year, place, price });
    }
    printwine(vecWine);
}

也就是说,你应该read more about std::vector 了解更多,它是如何工作的。

另外,很好读一下,如何重载operator&gt;&gt; and operator&lt;&lt;,这样你甚至可以编写代码,更简单。

以下是不完整的代码,我在涵盖我提到的主题后让您完成。

void printwine(const std::vector<Wine1>& vecWine)
{
    for (const auto& wine : vecWine)
    {
        std::cout << wine << '\n'; 
    }
}

int main()
{
    std::vector<Wine1> vecWine(wineNumber);
    for (Wine1& wine : vecWine)
    {
        std::cin >> wine;
    }
    printwine(vecWine);
}

【讨论】:

    【解决方案2】:

    我认为存在一个误解:您不希望您的结构 Wine1 包含一个向量,而是您想要一个 Wine1 的向量。

    我建议使用类似于以下的数据结构:

    struct Wine {
       string name;
       string year;
       string place;
       string price;
    };
    
    
    void printwinelist(vector<Wine>& list){
        for(Wine& w : list){
            printwine(w);
        }
    }
    vector<Wine> winelist;
    

    必须相应地重写 main 方法,以将其他对象附加到向量。

    虽然我怀疑我可以相应地重写您的代码,但对您来说更好的下一步是阅读一些使用的概念,例如向量。

    【讨论】:

      【解决方案3】:

      你可能想要这样的东西:

      #include <string>
      #include <iostream>
      #include <vector>
      using namespace std;
      
      struct Wine1
      {   //struct for Wine pssibly needs Vector
          string name;
          string year;
          string place;
          string price;
      };
      
      void printwine(Wine1 wine);
      
      int main()
      {
          vector<Wine1> wineinventory;
      
          // read 3 wines
      
          for (int i = 3; i < 10; i++)
          {
            Wine1 wine;
            string str; //input for data
            cout << "Please enter the data of the First wine: " << endl;
            cout << "Enter name: ";
            getline(cin, wine.name);
            cout << endl << "Enter year: ";
            getline(cin, wine.year);
            cout << endl << "enter country of creation: ";
            getline(cin, wine.place);
            cout << endl << "enter price: ";
            getline(cin, wine.price);
            cout << endl;
      
            cout << "your entered data: " << endl;
            printwine(wine);
      
            wineinventory.push_back(wine);  // store in vectore
          }    
      
          // print all wines in the vector
      
          for (int i = 0; i < wineinventory.size(); i++)
          {
            cout << "Wine " << i << ":" endl;
            printwine(wineinventory[i]);
          }
      }
      

      免责声明:这是未经测试的代码,我什至不确定它是否可以编译,但你应该明白。

      还有很大的改进空间。

      【讨论】:

      • 一个小错字:printwine(wineinventory[i]); 我猜你首先想实现一个for range
      猜你喜欢
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多