lunes, 23 de febrero de 2015

STL Lista de Estudiantes Visual C++



#include <iostream>
#include <cstdlib>
#include <list>
#include <string>

using namespace std;

class Student
{
public:

 Student( const char *pszS , int id ) : sName( pszS ) , ssID( id ) {}
 string sName;
 int ssID;
};

bool operator<( const Student &s1 , const Student &s2 )
{
 return s1.ssID < s2.ssID;
}

void displayStudent( list < Student > &students )
{
 auto iter = students.begin();

 while( iter != students.end() )
 {
  Student &s = *iter;
  cout << s.ssID << " - " << s.sName << endl;

  iter++; //se mueve al siguiente elemento de la iteracion
 }
}

int main( int argc , char *argv[] )
{
 //se define la coleccion de estudiantes
 list < Student > students;

 //Agrego 3 objetos estudiantes a la lista
 students.push_back( Student("Christian Kenny" , 10 ) );
 students.push_back( Student("Dariana" , 5 ) );
 students.push_back( Student("Claudia" , 15 ) );

 //muestro la lista
 cout << "La lista original es: " << endl;
 displayStudent( students );

 //Muestro la lista ordenada
 students.sort();
 cout << "\nLa lista ordenada es: " << endl;
 displayStudent( students );

 system("pause");
 return 0;
}


No hay comentarios:

Publicar un comentario