Sunteți pe pagina 1din 6

Program 1

#include <iostream>

#include <list>

using namespace std;

int main()

list<int> l;

l.push_front(5);

l.push_front(2);

l.push_front(4);

l.push_front(1);

l.push_back(3);

list<int>::iterator temp;

cout<<"List"<<endl;

for(temp=l.begin();temp!=l.end();temp++)

cout<<*temp<<endl;

cout<<endl<<l.front();

cout<<endl<<l.back();

l.pop_front();

l.pop_back();

cout<<endl<<"List"<<endl;

for(temp=l.begin();temp!=l.end();temp++)

cout<<*temp<<endl;

list<int>::iterator it;

it = l.begin();
l.insert(it,6);

cout<<endl<<"List"<<endl;

for(it=l.begin();it!=l.end();it++)

cout<<*it<<endl;

l.sort();

cout<<"Sorted List"<<endl;

for(temp=l.begin();temp!=l.end();temp++)

cout<<*temp<<endl;

list<int> l1;

l1.push_front(10);

l1.push_front(11);

l1.push_front(12);

l1.push_front(13);

l.merge(l1);

cout<<"Merged list"<<endl;

for(temp=l.begin();temp!=l.end();temp++)

cout<<*temp<<endl;

l.reverse();

cout<<"Reversed list"<<endl;

for(temp=l.begin();temp!=l.end();temp++)

cout<<*temp<<endl;

}
Program 2

// Standard Template Library example using a class.

#include <iostream>

#include <list>

using namespace std;

// The List STL template requires overloading operators =, == and <.

class time_UD

friend ostream &operator<<(ostream &, const time_UD &);

public:

int hr;

int min;

int sec;

time_UD();

time_UD(const time_UD &);

~time_UD(){};

time_UD &operator=(const time_UD &rhs);

int operator==(const time_UD &rhs) const;

int operator<(const time_UD &rhs) const;

};

time_UD::time_UD() // Constructor

hr = 0;

min = 0;

sec = 0;

// Copy constructor to handle pass by value.

time_UD::time_UD(const time_UD &copyin)

hr = copyin.hr;
min = copyin.min;

sec = copyin.sec;

ostream &operator<<(ostream &output, const time_UD &time_UD)

output << time_UD.hr

<< ' ' << time_UD.min << ' ' << time_UD.sec << endl;

return output;

time_UD& time_UD::operator=(const time_UD &rhs)

this->hr = rhs.hr;

this->min = rhs.min;

this->sec = rhs.sec;

return *this;

int time_UD::operator==(const time_UD &rhs) const

if( this->hr != rhs.hr) return 0;

if( this->min != rhs.min) return 0;

if( this->sec != rhs.sec) return 0;

return 1;

// This function is required for built-in STL list functions like sort

int time_UD::operator<(const time_UD &rhs) const

if( this->hr == rhs.hr && this->min == rhs.min


&& this->sec < rhs.sec) return 1;

if( this->hr == rhs.hr && this->min < rhs.min) return 1;

if( this->hr < rhs.hr ) return 1;

return 0;

main()

list<time_UD> L;

time_UD Ablob ;

Ablob.hr=7;

Ablob.min=2;

Ablob.sec=4;

L.push_back(Ablob);

// Insert a new element at the end

Ablob.hr=5;

L.push_back(Ablob);

// Object passed by value. Uses default member-wise

// copy constructor

Ablob.sec=3;

L.push_back(Ablob);

Ablob.hr=3;

Ablob.min=7;

Ablob.sec=7;

L.push_back(Ablob);

list<time_UD>::iterator i;

for(i=L.begin(); i != L.end(); ++i)

cout << (*i).hr << " ";

// print member

cout << endl;

for(i=L.begin(); i != L.end(); ++i)


cout << *i << " ";

// print with overloaded operator

cout << endl;

cout << "Sorted: " << endl;

L.sort();

for(i=L.begin(); i != L.end(); ++i)

cout << *i << " ";

// print with overloaded operator

cout << endl;

return 0;

S-ar putea să vă placă și