对象向量搜索字符串实体

时间:2018-12-08 16:38:57

标签: c++

我有一个库存类对象,在库存对象中存在3个用于InventoryName,Barcode和InventoryQuantity的实体。用户输入填充这些实体,并将对象传递给矢量进行存储。我的问题出在尝试搜索实体的向量时,我可以将其删除,请参见源代码和removeItem函数,我在向量上尝试了binary_search并收到错误,当我使用find()函数时,它给了我错误“二进制'=='未找到采用'const Stock'类型的左操作数的运算符”。到目前为止我的代码 我的班级头文件

//inventory.h
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>
#include <vector>


class Stock {
public:
// Initialise object of class Stock with data members
explicit Stock(std::string, std::string, std::string);

// Prototype for member function DisplayStckItem, Displays entire stock
void displayStckItem(std::string, std::string, std::string) const;

//function prototype to add items to the vector array invName (Setters)
void setItemName(std::string);
void setBarcode(std::string);
void setInvQty(std::string);

//getters for the data members housed in the vector
std::string getInventoryName() const;
std::string getBarcode() const;
std::string getInvQty() const;

// function to remove stock item based on matched arguments
void removeItem(const std::vector<Stock>&);

// function to display all stock
void displayTotal(const std::vector <Stock> &); 

private:
std::string InventoryName; //data member of stock name
std::string Barcode; // data member of stock barcodes
protected:
std::string InventoryQty; //data member for stock levels
};

来源

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <cmath>
#include <string>
#include <vector>
#include <regex>
#include "inventory.h"
using namespace std;


Stock::Stock(string code, string iName, string iQuantity)
    : Barcode(code), InventoryName(iName), InventoryQty(iQuantity)  //member initiailiser list
    {   

    };



void Stock::displayStckItem(string x, string y, string z) const {

}


//setters
void Stock::setItemName(string newItemName) {
    bool valiName;
    do {
        cout << "Please enter item name: " << endl;
        cin.ignore();
        getline(cin, newItemName);
        if (regex_match(newItemName, regex("^[A-Z a-z]+$")) == true && newItemName.size() <= 15)
            valiName = true;
        else {
            cout << "Invalid input, No numbers allowed in inventory name and only 15 cahracters long" << endl;
            valiName = false;
        }
    } while (valiName == false);
    InventoryName = newItemName;
}


void Stock::setBarcode(string newItemCode) {
    bool valiCode;
    do {
        cout << "Please enter barcode: " << endl;
        cin >> newItemCode;
        if (regex_match(newItemCode, regex("^[0-9]+$")) == true && newItemCode.size() == 6) {
            valiCode = true;
        }
        else {
            cout << "Invlaid input, values must be number values and 6 characters long" << endl;
            valiCode = false;
        }
    } while (valiCode == false);
    Barcode = newItemCode;
}


void Stock::setInvQty(string newItemQty) {
    bool valiQty;
    do {
        cout << "Please enter item quantity: " << endl;
        cin >> newItemQty;
        if (regex_match(newItemQty, regex("^[0-9]+$")) == true && newItemQty.size() <= 6) {
            valiQty = true;
        }
        else {
            cout << "Invalid input, input number value with max of 999,999" << endl;
        }
    } while (valiQty == false);
    InventoryQty = newItemQty;
}

void Stock::removeItem(const vector <Stock> &a) {
    string invItem; 
    cin.ignore();
    getline(cin, invItem);
    bool found = binary_search(a.begin(), a.end(), InventoryName);
    if (invItem == InventoryName) {
        cout << "Item found" << endl;
    }
    else {
        cout << "Item not found" << endl;
    }
}

void Stock::displayTotal(const vector <Stock> &a) {
    cout << "Items held in inventory" << endl;
    for (Stock const &item : a) {
        cout << item.getInventoryName() << setw(16) << item.getBarcode() << setw(8) << item.getInvQty() << endl;
    }
}
//getters
string Stock::getInventoryName() const{
    return InventoryName;
}
string Stock::getBarcode() const {
    return Barcode;
}
string Stock::getInvQty() const {
    return InventoryQty;
}

驱动程序

#include "inventory.h"
#include <vector>

using namespace std;


int main() {
    vector<Stock> range;
    string temp1;
    string temp2;
    string temp3;
    char options;
    bool prog; //boolean to repeat and exit main program
    //Instantiate stock object for passing to vector
    Stock Stock1(temp1, temp2, temp3);


    cout << "Welcome to your inventory management system!" << endl;

    do {
        cout << "\nPlease enter a number from the following options" << endl;

        cout << "To add a product: 1\nTo remove a product: 2\nTo veiw product inventory: 3\nTo exit the program: 4" << endl;


        cin >> options;

        if (options >= 49 && options <= 52) { // test char options within the ascii range between 1 and 4
            switch (options)
            {
            case '1':
                Stock1.setItemName(temp2);
                Stock1.setBarcode(temp1);
                Stock1.setInvQty(temp3);

                range.push_back(Stock1);
                cout << "New product added" << endl;
                prog = true;
                break;
            case '2': cout << "Please input a product name or barcode" << endl;
                Stock1.removeItem(range);
                prog = true;
                break;
            case '3': cout << "Veiw product" << endl;
                Stock1.displayTotal(range);
                prog = true;
                break;
            case '4': cout << "Exit program" << endl;
                prog = false;
                break;
            }
        }
        else {
            cout << "Your input is not within the options list" << endl;
        }
    } while (prog == true);

}

我提供了一切,因为我正在学习c ++和有关样式的反馈,否则我可能犯的任何其他错误都将是很棒的。希望我的帖子也能对其他人有所帮助。

2 个答案:

答案 0 :(得分:0)

您应该为“股票”类重载equals运算符:

class Stock {
    (...)
    bool operator==(const Stock& lhs, const Stock& rhs) {
        return lhs.InventoryName == rhs.InventoryName && lhs.Barcode == rhs.Barcode && lhs.InventoryQty == rhs.InventoryQty;
    }
    (...)
}

答案 1 :(得分:0)

所以我找到了解决这个问题的可能不太好的解决方法,我想搜索对象实体的向量并返回索引。

void Stock::removeItem(vector <Stock> &a) {
    string invItem, name;
    int i = 0;
    cin.ignore();
    getline(cin, invItem);
    for (Stock const &item : a) {
        name = item.getInventoryName();
        if (name == invItem) {
            cout << "item found:   " << i << endl;
            a.erase(a.begin() + i);
            break;
        }
        else if (a.empty()) {
            cout << "No items currently in inventory" << endl;
        }
        else {
            cout << "Item not in inventory" << endl;
            ++i;
        }
    }

}

如果这很浪费或很脆弱,我们欢迎您提供反馈。