Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <tchar.h>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <array>
- #include <deque>
- #include <list>
- #include <forward_list>
- #include <set>
- #include <unordered_set>
- #include <map>
- #include <unordered_map>
- // CLASS_05a: przyklady kontenerow STL
- // funkcja pomocnicza do drukowania zawartosci dowolnego kontenera
- template <typename T>
- void PrintContainer(T& cont)
- {
- for (auto& elem : cont) {
- std::cout << elem << " ";
- }
- std::cout << std::endl;
- }
- // funkcja pomocnicza do drukowania zawartosci pary elementow
- template <typename T1, typename T2>
- std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& pair) // output
- {
- out << "(" << pair.first << ", " << pair.second << ")";
- return out;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- // tablica z przykladowymi danymi
- int temp[6] = { 3, 5, 2, 6, 1, 4 };
- std::cout << "====== kontenery sekwencyjne ======" << std::endl << std::endl;
- std::vector<int> v;
- for (int i = 0; i < 6; ++i) {
- v.push_back(temp[i]);
- }
- std::cout << "vector - tablica dynamiczna o zmiennym rozmiarze, dostep losowy do zawartosci: " << std::endl;
- PrintContainer(v);
- // rozmiar kontenera "array" musi byc znany w czasie kompilacji i jest podany jako parametr szablonu
- std::array<int, 6> a;
- for (int i = 0; i < 6; ++i) {
- a[i] = temp[i];
- }
- std::cout << "array - tablica o stalym rozmiarze, dostep losowy do zawartosci: " << std::endl;
- PrintContainer(a);
- std::deque<int> d;
- for (int i = 0; i < 6; ++i) {
- d.push_front(temp[i]);
- }
- std::cout << "deque - lista dwukierunkowa z losowym dostepem do zawartosci: " << std::endl;
- PrintContainer(d);
- std::list<int> l;
- for (int i = 0; i < 6; ++i) {
- l.push_front(temp[i]);
- }
- std::cout << "list - lista dwukierunkowa z sekwencyjnym dostepem do zawartosci: " << std::endl;
- PrintContainer(l);
- std::forward_list<int> f;
- for (int i = 0; i < 6; ++i) {
- f.push_front(temp[i]);
- }
- std::cout << "forward_list - lista jednokierunkowa z sekwencyjnym dostepem do zawartosci: " << std::endl;
- PrintContainer(f);
- std::cout << std::endl << "====== kontenery asocjacyjne ======" << std::endl << std::endl;
- std::set<int> s;
- for (int i = 0; i < 5; ++i) {
- s.insert(temp[i]);
- s.insert(temp[i + 1]);
- }
- std::cout << "set - zbior uporzadkowany, przechowuje unikalne, posortowane elementy: " << std::endl;
- PrintContainer(s);
- std::multiset<int> ms;
- for (int i = 0; i < 5; ++i) {
- ms.insert(temp[i]);
- ms.insert(temp[i+1]);
- }
- std::cout << "multiset - zbior uporzadkowany, przechowuje posortowane elementy, ktore moga sie powtarzac: " << std::endl;
- PrintContainer(ms);
- std::map<int, std::string> m;
- for (int i = 0; i < 5; ++i) {
- m.insert(std::make_pair(temp[i], std::to_string(temp[i])));
- m.insert(std::make_pair(temp[i + 1], std::to_string(temp[i + 1])));
- }
- std::cout << "map - zbior uporzadkowany, przechowuje unikalne, posortowane po kluczu pary elementow: " << std::endl;
- PrintContainer(m);
- std::multimap<int, std::string> mm;
- for (int i = 0; i < 5; ++i) {
- mm.insert(std::make_pair(temp[i], std::to_string(temp[i])));
- mm.insert(std::make_pair(temp[i+1], std::to_string(temp[i+1])));
- }
- std::cout << "multimap - zbior uporzadkowany, przechowuje posortowane po kluczu pary elementow, klucze moga sie powtarzac: " << std::endl;
- PrintContainer(mm);
- std::cout << std::endl << "====== kontenery nieuporzadkowane ======" << std::endl << std::endl;
- std::unordered_set<int> us;
- for (int i = 0; i < 5; ++i) {
- us.insert(temp[i]);
- us.insert(temp[i + 1]);
- }
- std::cout << "unordered_set - zbior nieuporzadkowany, przechowuje unikalne elementy: " << std::endl;
- PrintContainer(us);
- std::unordered_multiset<int> ums;
- for (int i = 0; i < 5; ++i) {
- ums.insert(temp[i]);
- ums.insert(temp[i + 1]);
- }
- std::cout << "unordered_multiset - zbior nieuporzadkowany, przechowuje elementy, ktore moga sie powtarzac: " << std::endl;
- PrintContainer(ums);
- std::unordered_map<int, std::string> um;
- for (int i = 0; i < 5; ++i) {
- um.insert(std::make_pair(temp[i], std::to_string(temp[i])));
- um.insert(std::make_pair(temp[i + 1], std::to_string(temp[i + 1])));
- }
- std::cout << "unordered_map - zbior nieuporzadkowany, przechowuje unikalne pary elementow: " << std::endl;
- PrintContainer(um);
- std::unordered_multimap<int, std::string> umm;
- for (int i = 0; i < 5; ++i) {
- umm.insert(std::make_pair(temp[i], std::to_string(temp[i])));
- umm.insert(std::make_pair(temp[i+1], std::to_string(temp[i+1])));
- }
- std::cout << "unordered_multimap - zbior nieuporzadkowany, pary elementow, ktore moga sie powtarzac: " << std::endl;
- PrintContainer(umm);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement