简体   繁体   English

如何在矢量中搜索结构项?

[英]How Do I Search For Struct Items In A Vector?

I'm attempting to create an inventory system using a vector implementation, but I seem to be having some troubles. 我正在尝试使用矢量实现创建库存系统,但我似乎遇到了一些麻烦。 I'm running into issues using a struct I made. 我正在使用我制作的结构来解决问题。 NOTE: This isn't actually in a game code, this is a separate Solution I am using to test my knowledge of vectors and structs! 注意:这实际上不是游戏代码,这是一个单独的解决方案,我用来测试我对矢量和结构的知识!

struct aItem
{
    string  itemName;
    int     damage;
};

int main()
{
    aItem healingPotion;
    healingPotion.itemName = "Healing Potion";
    healingPotion.damage= 6;

    aItem fireballPotion;
    fireballPotion.itemName = "Potion of Fiery Balls";
    fireballPotion.damage = -2;

    vector<aItem> inventory;
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(fireballPotion);

    if(find(inventory.begin(), inventory.end(), fireballPotion) != inventory.end())
                {
                        cout << "Found";
                }

    system("PAUSE");
    return 0;
}

The preceeding code gives me the following error: 前面的代码给出了以下错误:

1>c:\\program files (x86)\\microsoft visual studio 11.0\\vc\\include\\xutility(3186): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'aItem' (or there is no acceptable conversion) 1> c:\\ program files(x86)\\ microsoft visual studio 11.0 \\ _vc \\ include \\ xutility(3186):错误C2678:二进制'==':找不到带有'aItem'类型左手操作数的运算符(或者没有可接受的转换)

There is more to the error, if you need it please let me know. 还有更多错误,如果您需要,请告诉我。 I bet it's something small and silly, but I've been thumping at it for over two hours. 我敢打赌,这是一件小而愚蠢的事,但我已经被它吵了两个多小时。 Thanks in advance! 提前致谢!

find looks for something that's equal to the item in the vector. find与向量中的项目相等的内容。 You say you want to search using strings, but you haven't written code for that; 你说你想使用字符串进行搜索,但是你没有为此编写代码; it's trying to compare the entire struct. 它试图比较整个结构。 And you haven't written code to compare entire structs, so it's giving you an error. 而且你还没有编写代码来比较整个结构,所以它给你一个错误。

The simplest solution is to use an explicit loop instead of find . 最简单的解决方案是使用显式循环而不是find

If you want to find things by string, use the find_if variant and write a predicate function that looks at the string. 如果find_if字符串find内容,请使用find_if变量并编写一个查看字符串的谓词函数。 Or if you want to find things by the entire struct you can define an operator == on the struct that compares both itemName and damage . 或者如果你想通过整个结构find东西,你可以在结构上定义一个operator == ,比较itemNamedamage

Or you might also consider using the map or unordered_map data structures instead of vector . 或者您也可以考虑使用mapunordered_map数据结构而不是vector The map containers are designed for fast lookup using a key (such as the string). 映射容器设计用于使用密钥(例如字符串)进行快速查找。

The find method does not know how to compare two aItem objects for equality. find方法不知道如何比较两个aItem对象的相等性。 You need to define the == operator in your struct definition, like this: 您需要在结构定义中定义==运算符,如下所示:

bool operator==(aItem other)
{
    if (itemName == other.itemName && damage == other.damage)
        return true;
    else
        return false;
}

This will allow find to determine if two aItem objects are equal, which is necessary for the algorithm to work. 这将允许find确定两个aItem对象是否相等,这是算法工作所必需的。

try something like: 尝试类似的东西:

#include <iostream>
#include <vector>
using namespace std;
struct item {
    item(string const name,int const damage):name_(name),damage_(damage) {

    }
    string name_;
    int damage_;
};
int main(int argc, char** argv) {
    vector<item *> items;
    item healingPostion("cure light",-10);
    item fireballPostion("fireball",10);
    items.push_back(&healingPostion);
    items.push_back(&fireballPostion);
    if(find(items.begin(), items.end(), &fireballPostion) != items.end()) {
        cout << "Found";
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM