使用迭代器中的库存中的项目计数器

Counter for items in an inventory using iterators

本文关键字:项目 计数器 迭代器      更新时间:2023-10-16

我对向量和迭代器有些新事物,我正在尝试弄清楚如何使用迭代器显示项目的。一个例子是您有5个苹果。我希望它输出" 5x Apple"或类似的东西。我不知道如何做到这一点。这是一个简单的代码,该代码将用户放入字符串以添加到库存中。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string item;
vector<string> inventory;
vector<string>::iterator iter;
int main()
{
    while(true){
       cin >> item;
       inventory.push_back(item);
       cout << "INVENTORY:n";
       for(iter = inventory.begin(); iter != inventory.end(); iter++)
       cout << *iter << endl;
    }
}

编辑:我正在尝试为游戏制作库存系统。这就是为什么我认为我可能需要一个迭代器。如果有一种比使用迭代器更好的方法来制定库存系统,请告诉我。抱歉,我应该澄清。

迭代器可让您通过容器进行迭代,但对您没有任何计算。

一个容器的size()告诉您容器中有多少个项目,但是如果您有不同类型的项目,则必须自己计算它们。

例如,说您有4个"apple"和1 "orange"

您必须查看输入的每个项目并根据需要对其进行计数,例如:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> inventory;
int numApples = 0;
int numOranges = 0;
int numOther = 0;
int main()
{
    string item;
    while (cin >> item)
    {
        inventory.push_back(item);
        if (item == "apples")
            ++numApples;
        else if (item == "orange")
            ++numOranges;
        else
            ++numOther;
    }
    cout << "INVENTORY:n";
    for (vector<string>::iterator iter = inventory.begin(); iter != inventory.end(); ++iter)
        cout << *iter << endl;
    /* or, if you are using C++11 or later:
    for (string &s : inventory)
        cout << s << endl;
    */
    cout << "# apples: " << numApples << endl;
    cout << "# oranges: " << numOranges  << endl;
    cout << "# other: " << numOther << endl;
    return 0;
}

或,您可以考虑使用std::count_if(),例如:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> inventory;
bool isApple(const string &s) { return (s == "apple"); }
bool isOrange(const string &s) { return (s == "orange"); }
bool isOther(const string &s) { return !(isApple(s) || isOrange(s)); }
int main()
{
    string item;
    while (cin >> item)
        inventory.push_back(item);
    cout << "INVENTORY:n";
    for (vector<string>::iterator iter = inventory.begin(); iter != inventory.end(); ++iter)
        cout << *iter << endl;
    /* or, if you are using C++11 or later:
    for (string &s : inventory)
        cout << s << endl;
    */
    cout << "# apples: " << count_if(inventory.begin(), inventory.end(), isApple) << endl;
    cout << "# oranges: " << count_if(inventory.begin(), inventory.end(), isOrange) << endl;
    cout << "# other: " << count_if(inventory.begin(), inventory.end(), isOther) << endl;
    /* or, if you are using C++11 or later:
    cout << "# apples: " << count_if(inventory.begin(), inventory.end(), [](auto &s){ return (s == "apple"); }) << endl;
    cout << "# oranges: " << count_if(inventory.begin(), inventory.end(), [](auto &s){ return (s == "orange"); }) << endl;
    cout << "# other: " << count_if(inventory.begin(), inventory.end(), [](auto &s){ return (s != "apple") && (s != "orange"); }) << endl;
    */
    return 0;
}

update :基于您发布的另一个问题,尝试更多类似的东西:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> other_inventory;
int numApples = 0;
int numOranges = 0;
int main()
{
    string item;
    while (cin >> item)
    {
        if (item == "apples")
            ++numApples;
        else if (item == "orange")
            ++numOranges;
        else
            other_inventory.push_back(item);
    }
    cout << "INVENTORY:n";
    if (numApples > 0)
        cout << "# apples: " << numApples << endl;
    if (numOranges > 0)
        cout << "# oranges: " << numOranges  << endl;
    for (vector<string>::iterator iter = other_inventory.begin(); iter != other_inventory.end(); ++iter)
        cout << *iter << endl;
    /* or, if you are using C++11 or later:
    for (string &s : other_inventory)
        cout << s << endl;
    */
    return 0;
}

简化这一点的一种方法是对库存中的项目进行排序。这将使相同的群体融合在一起,这简化了计算它们。从头开始,计算匹配当前项目的连续项目的数量,显示并继续使用第一个不匹配的项目。

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main()
{
    std::string item;
    std::vector<std::string> inventory;
    while (true) {
       std::cin >> item;
       if (item  == "quit") return 0;
       inventory.push_back(item);
       std::sort(inventory.begin(), inventory.end());
       std::cout << "INVENTORY:n";
       auto current = inventory.begin();
       while (current != inventory.end()) {
          int count = 1;
          auto probe = current + 1;
          while (probe != inventory.end() && *probe == *current) { 
              ++count;
              ++probe;
          }
          std::cout << count << "x " << *current << 'n';
          current = probe;
       }
       std::cout.flush();
    }
    return 0;
}

更详细地说,如果您的库存为{"橙色","苹果","橙色"},则排序将使顺序重新布置为{"苹果","橙色","橙色"}。请注意,相同的相同。

现在迭代器current从开头开始(" Apple")。我们将count设置为1,因为我们知道至少有1。我们将迭代器probe设置为指向下一个项目("橙色")。由于probe处的值与current处的值不匹配,因此内部循环无能为力。我们打印count和当前项目(" Apple")。我们继续将current设置为probe,因为此时,probe将指向第一个与当前一个不匹配的项目。

在第二次迭代中,current指的是第一个"橙色"。我们将count重置为1,然后在下一个项目("橙色")开始probe。由于匹配时值,我们会增加count(现在2)并提前probe(现在在库存结束时)。我们打印count(2)和当前项目("橙色"),然后将current设置为probe(列表的结尾)。外环条件看到我们处于库存的末端,因此循环终止。

inventory.size()

返回矢量中的项目数。

我看不出您将如何完成该任务的迭代器。