使用迭代器中的库存中的项目计数器
Counter for items in an inventory using iterators
我对向量和迭代器有些新事物,我正在尝试弄清楚如何使用迭代器显示项目的量。一个例子是您有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()
返回矢量中的项目数。
我看不出您将如何完成该任务的迭代器。
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 循环在计数器中不起作用
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 计数器将项目添加到数组中,并剩余
- 使用迭代器中的库存中的项目计数器
- 内存计数器 - 碰撞检测项目