//Maciej Czerniak 4I2

#include <set>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc,char** argv)
{

    // Deklaracja zbioru z elementami typu int
    set<int> set1;

    cout<<"Dodawanie:";
    srand(NULL);
    for(int i=0;i<10;i++)
    {
        int value=rand()%200;
	cout<<" "<<value;
	// Dodajemy 10 losowych elementów do set1.
	set1.insert(value);
    }
    cout<<endl<<endl;

    // Deklaracja iteratora wskazującego na początek zbioru
    set<int>::iterator it1 = set1.begin();

    // Wyświetlenie wszystkich elementów
    cout<<"set1=";
    do
    {
	cout<<*it1<<" ";
        it1++;
    }
    while(it1!=set1.end());
    cout<<endl;

    it1 = set1.begin();
    set<int>::iterator it2 = set1.end();

    //it1 wskazuje na drugi element
    it1++;
    //it2 wskazuje na ostatni element
    it2--;

    // set2 staje się kopią elementów między drugim a ostatnim elementem set1
    set<int> set2(it1,it2);

    // wyświetlenie set2
    cout<<"set2=";
    set<int>::iterator it3 = set2.begin();
    do
    {
	cout<<*it3<<" ";
        it3++;
    }
    while(it3!=set2.end());
    cout<<endl<<endl;

    // set3 staje się kopią set2
    set<int> set3(set2);

    // wykorzystanie reverse iterator - wyświetlenie w odwrotnej kolejności
    cout<<"reverse set3=";
    set<int>::reverse_iterator it4 = set3.rbegin();
    do
    {
	cout<<*it4<<" ";
        it4++;
    }
    while(it4!=set3.rend());
    cout<<endl<<endl;

    cout<<"set4 deklaracja"<<endl;
    set<int> set4;
    // Sprawdzenie czy zbiór jest pusty
    cout<<"set4.empty()="<<set4.empty()<<endl;
    cout<<"set4 insert 6"<<endl;
    // Dodanie elementu
    set4.insert(6);
    cout<<"set4.empty()="<<set4.empty()<<endl;
    cout<<"set4 erase 6"<<endl;
    // Usunięcie elementu o podanej wartości
    set4.erase(6);
    cout<<"set4.empty()="<<set4.empty()<<endl<<endl;

    // Ilosc elementow w zbiorze
    cout<<"set3.size()="<<set3.size()<<endl;
    cout<<"set4.size()="<<set4.size()<<endl<<endl;

    // maksymalna ilość elementów ze względu na ograniczenia systemu, biblioteki
    cout<<"set3.maxsize()="<<set3.max_size()<<endl<<endl;


    set<int>::iterator it5;
    it5=set2.begin();
    cout<<"set2=";
    do
    {
	cout<<*it5<<" ";
        it5++;
    }
    while(it5!=set2.end());
    it5=set2.begin();
    it5++;
    it5++;
    // Usuwanie elementu wskazywanego przez iterator
    cout<<endl<<"Usuwamy 3 element"<<endl;
    set2.erase(it5);
    it5=set2.begin();
    cout<<"set2=";
    do
    {
	cout<<*it5<<" ";
        it5++;
    }
    while(it5!=set2.end());
    cout<<endl<<endl;

    // Usuwanie zakresu wskazywanego przez iteratory
    cout<<"Usuwanie zakresu"<<endl;
    set2.erase(set2.begin(),--(set2.end()));
    it5=set2.begin();
    cout<<"set2=";
    do
    {
	cout<<*it5<<" ";
        it5++;
    }
    while(it5!=set2.end());
    cout<<endl<<endl;

    //zbior zawierajacy liczby od 0 do 9
    set<int> set5;
    for(int i=0;i<10;i++)
	set5.insert(i);

    //Wyszukiwanie elementu o podanej wartości
    cout<<"Wyszukiwanie"<<endl;
    set<int>::iterator it6=set5.find(5);
    // Gdy nie znaleziono iterator wskazuje na end
    if(it6!=set5.end()) cout<<*it6<<endl; else cout<<"Not found"<<endl;
    it6=set5.find(50);
    if(it6!=set5.end()) cout<<*it6<<endl; else cout<<"Not found"<<endl;

    //Wyszukiwanie elementu najbliższego zadanemu
    //lower_bound zwraca element większy bądź równy podanemu
    //upper_bound zwraca element więkdzy od podanego
    cout<<"lower/upper bound"<<endl;
    it1=set1.begin();
    cout<<"set1=";
    do
    {
	cout<<*it1<<" ";
        it1++;
    }
    while(it1!=set1.end());
    cout<<endl;

    cout<<"lower_bound="<<*(set1.lower_bound(50))<<" upper_bound="<<*(set1.upper_bound(100))<<endl;

    return 0;
}
