Elementy listy

0

Witam, mam dwa problemy z listą
Po pierwsze chciałbym przejść po wszystkich elementach listy, poza pierwszym. Zapis lista.begin()+1 jest zły, więc chciałbym się dowiedzieć jak to zrobić?
Drugi, mam funkcje średnia(), która oblicza średnią i chcę porównywać elementy, aby wyłonić ten z najlepszą średnią i przy próbie odwołania się do poprzedniego elementu (v-1)->srednia(); również mam błąd. Pomocy!

				for (auto v=studenci.begin()+1; v!=studenci.end(); ++v)
				{
					if (v->srednia()>(v-1)->srednia())
					{
						licznik_1=licznik_2+1;
					}

					licznik_2++;

				}
 
2
#include <list>
#include <iostream>
using namespace std;

struct Student
  {
   double _srednia;
   double srednia()const { return _srednia; }
  };

int main()
  {
   list<Student> studenci;
   size_t pos=(size_t)-1,best=pos;
   auto curr=studenci.begin();
   if(curr!=studenci.end()) for(auto prev=curr++;curr!=studenci.end();prev=curr++,++pos) if(curr->srednia()>prev->srednia()) best=pos;   
   return 0;
  }

Aczkolwiek wg mnie nie potrzebujesz numeru (bo dla listy nić nie daje) potrzebujesz iteratora.

#include <list>
#include <iostream>
using namespace std;

struct Student
  {
   double _srednia;
   double srednia()const { return _srednia; }
  };

int main()
  {
   list<Student> studenci;
   auto curr=studenci.begin(),best=studenci.end();
   if(curr!=best) for(auto prev=curr++;curr!=studenci.end();prev=curr++) if(curr->srednia()>prev->srednia()) best=curr;
   if(best!=studenci.end()) cout<<best->srednia()<<endl;
   else cout<<"Brak studentow na liscie"<<endl;
   return 0;
  }
3

Zobacz std::next

A listy używaj tylko kiedy musisz (albo gdy benchmark jednoznacznie potwierdzi jej wyższość)

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