max_element i klasy

0

Hej. Mam problem ze znalezieniem największego elementu w vektorze.


class structure{
	  private:
	  		  vector <basic> tab_in;
			  vector <basic> tab_out;
	  public:
			  void set_state(int);
			  bool cmp(basic&,basic&);	
			  int max_el(structure &); 
}
class basic{
	  private:
	  		 int x,y,state;
	  public:
	  		  
			  basic();
			 
			  void set_xy(int,int);
			  void set_state(int);	  
			  int ret_x();
			  int ret_y();
			  int ret_state();		  
	  };

w funkcji int max_el(structure &); chciałabym znaleŹĆ najwiekszy element state z vector <basic> tab_in;

int structure::max_el(structure &a){
	int max_max = (*max_element(tab_in.begin(), tab_in.end(), cmp )).ret_state();


    cout << max << endl ;
return max;

	};
0

No a co wypluwa kompilator??

0

"cmp" musi być metodą statyczną albo funkcją.

0

@up niekoniecznie.
Można poprawić tak i też powinno zadziałać:

class structure{
...
                          bool operator()(basic&,basic&); // zamiast: bool cmp(basic&,basic&);       
...
};

int structure::max_el(structure &a){
        int max_max = (*max_element(tab_in.begin(), tab_in.end(),*this)).ret_state();

    cout << max << endl ;
return max;
        };
0
MarekR22 napisał(a)

bool operator()(basic&,basic&);
Jeżeli już, to albo

bool operator()(basic&)

albofriend bool operator()(basic&,basic&)


Trzecią możliwością jest oddzielna struktura, ze zdefiniowanym operatorem:
```cpp
bool operator() (basic&,basic&)
0

dziękuję za pomoc. działa :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1