Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用迭代器对库存中的项目进行计数器_C++_Vector - Fatal编程技术网

C++ 使用迭代器对库存中的项目进行计数器

C++ 使用迭代器对库存中的项目进行计数器,c++,vector,C++,Vector,我对向量和迭代器有些陌生,我正试图弄清楚如何使用迭代器显示大量的项。举个例子,你有5个苹果。我希望它能输出“5x苹果”之类的东西。我不知道如何才能做到这一点。下面是一个简单的代码,它让用户输入一个字符串以添加到清单中 #include <iostream> #include <string> #include <vector> using namespace std; string item; vector<string> inventory;

我对向量和迭代器有些陌生,我正试图弄清楚如何使用迭代器显示大量的项。举个例子,你有5个苹果。我希望它能输出“5x苹果”之类的东西。我不知道如何才能做到这一点。下面是一个简单的代码,它让用户输入一个字符串以添加到清单中

#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;
    }
}
#包括
#包括
#包括
使用名称空间std;
字符串项;
病媒清查;
向量:迭代器iter;
int main()
{
while(true){
cin>>项目;
库存。推后(项目);
库特
返回向量中的项数


我看不出您如何需要迭代器来完成该任务。

迭代器允许您在容器中进行迭代,但它不会对您进行任何计算

容器的
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;
}

更新:根据您发布的内容,尝试以下内容:

#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;
}
#包括
#包括
#包括
使用名称空间std;
向量其他_库存;
int numaples=0;
int numOranges=0;
int main()
{
字符串项;
而(cin>>项目)
{
如果(项目==“苹果”)
++无核;
否则如果(项目==“橙色”)
++努莫兰吉斯;
其他的
其他库存。推回(项目);
}
cout(0)

cout一种简化方法是对库存中的项目进行排序。这将把相同的项目放在一起,从而简化计数。从一开始,计算与当前项目匹配的连续项目数,显示它,然后继续第一个不匹配的项目

#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;
}
#包括
#包括
#包括
#包括
int main()
{
std::字符串项;
病媒清单;
while(true){
标准::cin>>项目;
如果(项目==“退出”)返回0;
库存。推后(项目);
排序(inventory.begin(),inventory.end());

STD::这与迭代器有什么关系?它使用迭代器,我认为这足以考虑它与迭代器有关。也许更多的是与向量有关的吗?你是判断者。为什么你在这里使用一个迭代器?而“5X苹果”的代码< 5 > /代码>在哪里?来自哪里?真的不清楚问题是什么is@UnholySheep这只是我想要输出的一个例子;如果用户输入“apple”5次,它将输出“5x apple”。全局变量通常是一件坏事,您应该尽量少用。变量的作用域越小,代码就越容易编写。对于迭代器来说更是如此。我无法想象任何情况下迭代器类型的全局变量会有用。谢谢您的回答。我将如何实现size()和/或CurtTyfIf()到代码中?它们是标准C++向量类型的一部分。我想在尝试这个任务之前需要了解更多。你不会用lambda函数代替正常进程吗?<代码> CurtTyIF(清单.No.*),Stutural.Enter(),[](AutoSs){Se= =“Apple”} < /C>?(在这种情况下,
auto
会起作用吗?我认为一个正确的答案相当于C#LINQ
GroupBy
@NetMage一个lambda也会起作用。我只是不想假设每个人都在使用C++11或更高版本的编译器。先生,你是个天才。这非常有效!谢谢!
#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;
}