如何通过对象向量搜索匹配项?

时间:2019-01-22 00:35:03

标签: c++ loops vector

我正在构建一个程序,在其中创建对象向量,并从.txt文件中读取创建对象的输入变量。我要检查用户引入的对象的“ ID”是否存在才能继续。

vector<Producto> vectorProductos;

while(file >> sID >> sDesc >> sUMed >> sFam >> sClass >> dVolumen >> dLongitud >> sPrecio){
    vectorProductos.push_back(Producto(sID, sDesc, sUMed,sFam, sClass, dVolumen, dLongitud, stringToDouble(sPrecio)));
    iNumProductos++;
}
file.close();

int iNumPartidas;
cout << "iNumPartidas? " << endl;
cin >> iNumPartidas;

for(unsigned int iP = 1; iP <= iNumPartidas; iP++){
    cout << endl << "Partida " << iP << ":" << endl;
    cout << "Clave de partida:\t";
    cin >> sPartida;
    for(unsigned int iPrd = 0; iPrd < iNumProductos; iPrd++){
        cout << endl << "Clave de producto " << iPrd+1 << ":\t";
        cin >> sClave;
        if(sClave == vectorProductos[iPrd].getClave()){
            cout << endl << "Cantidad:\t";
            cin >> iCantProdxP;
        }else{
            cout << "Producto no existe" << endl;
        }
    }
}

Producto

#ifndef Producto_h
#define Producto_h

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

class Producto{
protected:
    string _sClave, _sDescripcion, _sUMed, _sClass, _sFam;
    double _dVolumen, _dPrecio, _dLongitud;

public:
    Producto();
    Producto(string, string, string, string, string, double, double, double);
    inline string getClave(){return _sClave;}
    inline string getDescripcion(){return _sDescripcion;}
    inline string getUMed(){return _sUMed;}
    inline string getFam(){return _sFam;}
    inline string getClass(){return _sClass;}
    inline double getVol(){return _dVolumen;}
    inline double getPrecio(){return _dPrecio;}
    inline double getLongitud(){return _dLongitud;}
    inline void setClave(string sClave){_sClave = sClave;}
    inline void setDescripcion(string sDescripcion){_sDescripcion = sDescripcion;}
    inline void setUMed(string sUMed){_sUMed = sUMed;}
    inline void setFam(string sFam){_sFam = sFam;}
    inline void setClass(string sClass){_sClass = sClass;}
    inline void setVol(double dVolumen){_dVolumen = dVolumen;}
    inline void setPrecio(double dPrecio){_dPrecio = dPrecio;}
    inline void setLongitud(double dLongitud){_dLongitud = dLongitud;}
    void toString();
};

Producto::Producto(){
    _sClave = "";
    _sDescripcion = "Falta descripcion";
    _sUMed = "N/A";
    _sFam = "Sin Familia";
    _sClass = "Sin clase";
    _dVolumen = 0.0;
    _dPrecio = 0.0;
    _dLongitud = 0.0;
}

Producto::Producto(string sClave, string sDescripcion, string sUMed, string sFam, string sClass, double dVolumen, double dLongitud, double dPrecio){
    _sClave = sClave;
    _sDescripcion = sDescripcion;
    _sUMed = sUMed;
    _sFam = sFam;
    _sClass = sClass;
    _dVolumen = dVolumen;
    _dPrecio = dPrecio;
    _dLongitud = dLongitud;
}

void Producto::toString(){
    cout << "\nProducto: " << _sClave;
    cout << "\nDescripcion: " << _sDescripcion;
    cout << "\nUnidad de Medida: " << _sUMed;
    cout << "\nFamilia: " << _sFam;
    cout << "\nClase: " << _sClass;
    cout << "\nVolumen: " << _dVolumen;
    cout << "\nLongitud: " << _dLongitud;
    cout << "\nPrecio: " << _dPrecio;
    cout << endl;
}

我需要查看用户要输入的“ ID”是否确实存在,如果没有标记错误。当我运行程序时,我必须输入第一个产品的ID以使其与循环的索引号匹配,这是它唯一的工作方式,但是我需要程序将其与任何“ Producto”匹配位置或索引。

1 个答案:

答案 0 :(得分:1)

在内部循环中,您将在每次循环迭代时提示用户输入ID,然后将该ID仅与当前循环迭代索引处的乘积进行比较。您需要在进入循环之前提示用户一次,然后将ID与每个产品进行比较,直到找到匹配项:

std::cout << endl << "Clave de producto:\t";
std::cin >> sClave;
bool found = false;

for (std::size_t iPrd = 0; iPrd < vectorProductos.size(); ++iPrd)
{
    if (sClave == vectorProductos[iPrd].getClave())
    {
        found = true;
        break;
    }
}

if (found) {
    // do something ...
}
else {
    std::cout << "Producto no existe" << std::endl;
}

或者,您应该使用std::find_if()而不是手动搜索,例如:

Producto.h

#ifndef Producto_h
#define Producto_h

#include <iostream>
#include <string>

class Producto
{
protected:
    std::string _sClave, _sDescripcion, _sUMed, _sClass, _sFam;
    double _dVolumen, _dPrecio, _dLongitud;

public:
    Producto();
    Producto(std::string, std::string, std::string, std::string, std::string, double, double, double);

    std::string getClave() const { return _sClave; }
    std::string getDescripcion() const { return _sDescripcion; }
    std::string getUMed() const { return _sUMed; }
    std::string getFam() const { return _sFam; }
    std::string getClass() const { return _sClass; }
    double getVol() const { return _dVolumen; }
    double getPrecio() const { return _dPrecio; }
    double getLongitud() const { return _dLongitud; }

    void setClave(std::string sClave) { _sClave = sClave; }
    void setDescripcion(std::string sDescripcion) { _sDescripcion = sDescripcion; }
    void setUMed(std::string sUMed) { _sUMed = sUMed; }
    void setFam(std::string sFam) { _sFam = sFam; }
    void setClass(std::string sClass) { _sClass = sClass; }
    void setVol(double dVolumen) { _dVolumen = dVolumen; }
    void setPrecio(double dPrecio) { _dPrecio = dPrecio; }
    void setLongitud(double dLongitud) { _dLongitud = dLongitud; }

    std::string toString() const;
};

std::istream& operator>>(std::istream &in, Producto &p);

#endif

Producto.cpp

#include "Producto.h"
#include <sstream>

std::istream& operator>>(std::istream &in, Producto &p)
{
    std::string sID, sDesc, sUMed, sFam, sClass, sPrecio;
    double dVolumen, dLongitud;

    if (in >> sID >> sDesc >> sUMed >> sFam >> sClass >> dVolumen >> dLongitud >> sPrecio)
    {
        p.setClave(sID);
        p.setDescripcion(sDesc);
        p.setUMed(sUMed);
        p.setFam(sFam);
        p.setClass(sClass);
        p.setVol(dVolumen);
        p.setLongitud(dLongitud);
        p.setPrecio(stringToDouble(sPrecio));
    }
    return in;
}

Producto::Producto() :
    _sClave(),
    _sDescripcion("Falta descripcion"),
    _sUMed("N/A"),
    _sFam("Sin Familia"),
    _sClass("Sin clase"),
    _dVolumen(0.0),
    _dPrecio(0.0),
    _dLongitud(0.0)
{
}

Producto::Producto(std::string sClave, std::string sDescripcion, std::string sUMed, std::string sFam, std::string sClass, double dVolumen, double dLongitud, double dPrecio) :
    _sClave(sClave),
    _sDescripcion(sDescripcion),
    _sUMed(sUMed),
    _sFam(sFam),
    _sClass(sClass),
    _dVolumen(dVolumen),
    _dPrecio(dPrecio),
    _dLongitud(dLongitud)
{
}

std::string Producto::toString() const
{
    std::ostringstream oss;
    oss << "Producto: " << _sClave;
    oss << "\nDescripcion: " << _sDescripcion;
    oss << "\nUnidad de Medida: " << _sUMed;
    oss << "\nFamilia: " << _sFam;
    oss << "\nClase: " << _sClass;
    oss << "\nVolumen: " << _dVolumen;
    oss << "\nLongitud: " << _dLongitud;
    oss << "\nPrecio: " << _dPrecio;
    return oss.str();
}

主要

#include <vector>
#include <algorithm>
#include "Producto.h"

std::vector<Producto> vectorProductos;

Producto p;
while (file >> p) {
    vectorProductos.push_back(p);
}
file.close();

...

std::string sClave;

std::cout << std::endl << "Clave de producto:\t";
std::cin >> sClave;

auto iter = std::find_if(vectorProductos.begin(), vectorProductos.end(),
    [&](const Producto &p){ return (p.getClave() == sClave); });

if (iter == vectorProductos.end()) {
    std::cout << "Producto no existe" << std::endl;
}
else {
    std::cout << "Producto existe" << std::endl;

    // use *iter as needed...
    std::cout << iter->toString() << std::endl;

    // if you need the index of the found product, you can use:
    // auto index = std::distance(vectorProductos.begin(), iter);
}

...