1

I'm new of c++ programming. I need to find a specific object inside a vector of custom objects, but this vector is inside another class object. So i think i need to define a custom operator == but I don't know how and especially where. Please help me, I'm getting crazy. Hope I've been clear in my explanation. Below the header file of my classes.

#ifndef MAGAZZINO_H
#define MAGAZZINO_H

#include <iostream>
#include <string>
#include <vector>
#include "new_farmaco_magazzino.h"
#include "sacca.h"
#include "ricetta.h"
#include <algorithm>




using namespace std;

class farmaco_magazzino;
class sacca;
class ricetta;

struct index_detect{
    int index_large;
    int index_small;
};

struct da_scaricare{
    string ID_drug;
    eLiquid ID_bag;
    int num_vial;
    double capacity;
};

struct possible_scarico{
    vector<farmaco_magazzino> vial_large;
    vector<farmaco_magazzino> vial_small;
    vector<sacca> sacche_large;
    vector<sacca> sacche_small;
};

class magazzino {

public:

   vector<farmaco_magazzino> lista_small;
   vector<farmaco_magazzino> lista_large;
   vector<sacca> SaccheSmall; 
   vector<sacca> SaccheLarge;

   int new_capienza_SaccheLarge; //indica il numero di postazioni totali per le    sacche large
   int new_capienza_SaccheSmall; //indica il numero di postazioni totali per le sacche small
   int new_capienza_SiringheLarge; //indica il numero di postazioni totali per le siringhe large
   int new_capienza_SiringheSmall; //indica il numero di postazioni totali per le siringhe small
   int new_capienza_VialLarge; // indica il numero di postazioni totali per i vial large
   int new_capienza_VialSmall; // indica il numero di postazioni totali per i vial small

   int pos_disp_FarmacoLarge;
   int pos_disp_FarmacoSmall;
   int pos_disp_SaccheLarge;
   int pos_disp_SaccheSmall;
   int pos_disp_SiringheLarge;
   int pos_disp_SiringheSmall;

   //scarico possibile sulla base dei mismatch valido per magazzino di stoccaggio
   possible_scarico download;


   //default constructor
   magazzino();
   //overload constructor
   magazzino(int);
   //destructor
   ~magazzino();

   //accessor function
   int getNum_sacche(string); //restituisce il numero di sacche contenute nel magazzino
   //int getPos_totali(); //restituisce la capienza del magazzino
   int get_NumSiringhe(string);//restituisce il numero di siringhe contenute nel magazzino
   int getPos_disponibili(string,string); //restituisce le postazioni disponibili del magazzino
   //void setPos_disponibili(int);

   //mutator function
   void setNum_sacche(string,int);
   void setNum_siringhe(string,int);
   void update_magazzino(double*,vector<index_detect>&,magazzino&,string);//per magazzino interno
   bool update_magazzino(ricetta&,vector<index_detect>&,magazzino&,magazzino&,string);// per magazzino stoccaggio
   void load_magazzino(magazzino&,string,double,double,string,string,int);//carico magazzino interno
   void load_magazzino(magazzino&,string,double,string,string,int);// carico magazzino stoccaggio
   int postazioni_libere(int,vector<farmaco_magazzino>&);
   int num_object(int,int); // calcola il numero di sacche/siringhe contenute nel magazzino
   bool use_vial(double*,magazzino&,int,string); //per magazzino interno
   bool use_vial(ricetta&,magazzino&,magazzino&,int,string); //per magazzino stoccaggio
   bool use_bag(double*,magazzino&,int,string);//per magazzino interno
   bool use_bag(ricetta&,magazzino&,magazzino&,int,string);// per magazzino stoccaggio
   bool check_empty(string);
   vector<index_detect> find_lista(magazzino&,string,eContainerType); //trova elemento all'interno della lista dei vials oppure della sacche
   void svuota(magazzino&); //funzione di svuotamento del magazzino interno
   void clear_null(magazzino&,string); //elimina farmaci finiti
   void sorting(vector<double>&); //ordine i vettori di capacità o residual all'interno dello stesso farmaco
   void sorting_bag(vector<sacca>&,string);//ordine i vettori di capacità all'interno del vettore saccche



  // int magazzino::calcolo_pos_libere(int,int,vector<int>); //calcola il numero di postazioni libere
private:
   int new_num_SaccheLarge; // numero di sacche con capacità compresa tra 250cc e 500cc
   int new_num_SaccheSmall; // numero di sacche con capacità <250cc
   int new_NumSiringheLarge; // siringhe da 50ml utilizzate per dispensare farmaco in sacca
   int new_NumSiringheSmall; // siringhe da 5ml utilizzate come contenitore di destinazione


};

bool quantità_nulla(farmaco_magazzino&);


#endif

This is the main class which contains 2 vector of type farmaco_magazzino where I need to find a specific element; here the code of the class farmaco_magazzino.

  #ifndef NEW_FARMACO_MAGAZZINO_H
  #define NEW_FARMACO_MAGAZZINO_H

  #include <iostream>
  #include <string>
  #include "magazzino.h"

  using namespace std;

  enum eContainerType {
      _vial,
      _bag
  };



class farmaco_magazzino {
public:

    // default constructor
    farmaco_magazzino();
    //overload constructor
    farmaco_magazzino(string,double,eContainerType,int);
    // destructor
    ~farmaco_magazzino();

    vector<double> capacity; // capacità dei contenitori in cui è presente il farmaco (vial o bag)
    vector<double> residual; // farmaco residuo nei contenitori di destinazione nel caso del magazzino interno

    //accessor functions

    string get_IDfarmaco(); //restituisce ID farmaco contenuto in magazzino
    double get_quantità_farmaco(); //restituisce quantità di farmaco contenuta in magazzino
    eContainerType get_container_type(); //restituisce il tipo di contenitore in cui è contenuto il farmaco
    int get_num_vials(int); //restituisce il numero di vials di tipo 1/2 dello specifico  farmaco contenuto nel magazzino
    double getPriority(); //restituisce la priorità associata alla presenza in magazzino del farmaco
    int get_vial_used(); //restituisce il tipo di vial utilizzato per la ricetta i-esima

    //mutator functions

    void farmaco_magazzino::setPriority(double);
    void farmaco_magazzino::set_quantità_farmaco(double);
    void farmaco_magazzino::set_num_vials(int); //incrementa il numero di vials di tipo 1 e 2
    void farmaco_magazzino::set_vial_used(int);

    int new_num_Vial;//numero vials 
    //int new_num_VialSmall; //numero vials di tipo small


private:
    string new_IDfarmaco; //definire tipo enum
    double new_quantità_farmaco;
    eContainerType new_container_type; // se farmaco è contenuto in vial oppure in sacca
    double new_priority;
    int new_vial_used;   // new_vial_used=1->vial large; new_vial_used=2->vial small
};


#endif

Thank you for your help.

mlatu
  • 159
  • 1
  • 12

1 Answers1

0

i would suggest using maps instead of vectors. that ID you get from get_IDfarmaco is hopefully unique, if so you can use it as the key for the map. In fact if you want to use find, you will need to use a map. since vector doesnt have that method. or are you using something like boost?

if its stl::vector, then you would need to iterate through the vector and compare every object on its own which is O=n if im not mistaken. with map you would have O=log(n)

mlatu
  • 159
  • 1
  • 12
  • I used the method find on vectors in other situations;actually I think define a vector<> – user3173091 Feb 07 '14 at 11:07
  • Ok, you are right, there is std::find, but that is not a method of vector but a function, and its complexity is still linear between first and last iterators given, instead of logarithmic on a map – mlatu Feb 07 '14 at 11:12
  • I used the method find on vectors in other situations;actually I think i have to define a vector::iterator iter and then use find as iter=find(myvector.begin,myvector.end(),my_predicate); the definition of my predicate I think it would be something like an overload operator bool operator==(const myClass& other){return *this==other}; but it doesn't work and moreover i tried to define my predicate inside the class "farmaco_magazzino" but I don't know how to access it from the main class "magazzino" in the find command. – user3173091 Feb 07 '14 at 11:19
  • Sorry if i'm not considering maps but i'm new on c++ programming and i never used it. – user3173091 Feb 07 '14 at 11:20
  • Well, good luck on your == overloading. Maps are not that difficult to use really. the first element needs to be unique, thats all. and when you got that, you can use that first element in map::find() to get an iterator. Here is an example: (while some people dont like this site, i never had any problems with it) http://www.cplusplus.com/reference/map/map/find/ – mlatu Feb 07 '14 at 11:26