Commit 3af8bc21 authored by Standa Vitek's avatar Standa Vitek
Browse files

new files

parent 78b1e0bb
/*
prace s kontejnerem std::map
*/
#include <iostream>
#include <iomanip>
#include <map>
#define MULTIMAP
int main()
{
#ifndef MULTIMAP
// potrebuji par klic - hodnota
std::map<std::string, int> M;
M["Praha"] = 1300000;
M["Brno"] = 800000;
// pokud zaznam dosud neexistoval, vlozi se novy
M["Praha"] = 1000000;
M["Brno"] = 380000;
M["Budejovice"] = 100000;
M["As"] = 20000;
// map neumoznuje duplicitni klice
M["Praha"] = 500000;
#else
std::multimap<std::string, int> M = {{"Budejovice", 100000}, {"Ostrava", 900000}};
M.insert({"Praha", 1300000});
M.insert({"Plzen", 600000});
M.insert({"Brno", 800000});
M.insert({"Plzen", 750000});
#endif
// existujici zaznam se zmeni
M["Praha"] = 1200000;
// alternativne lze vlozit pomoci metody insert
M.insert({"Plzen", 250000});
// co se stane, kdyz polozka jiz existuje?
M.insert({"Praha", 1000000});
// prochazeni kontejneru
for (auto i : M)
{
std::cout << std::left << std::setw(12) << i.first << " " << i.second << std::endl;
}
std::cout << "\nhledam prvni vystkyt Plzen" << std::endl;
// najde prvni vyskyt paru s klicem "Plzen"
std::string x(21, '-');
std::cout << x << "\n";
for (auto m : M)
std::cout << std::left << std::setw(12) << m.first << "|" << std::setw(8) << std::right << m.second << std::endl;
std::cout << x << "\n";
// nalezeni polozky v kontejneru metodou find
// navratovou hodnotou je iterator
auto i = M.find("Plzen");
// navratova hodnota je iterator (-> ukazatel)
std::cout << std::left << std::setw(12) << i->first << " " << i->second << std::endl;
// protoze je kontejner razen podle klice, lze pak prozkoumat jeste dalsi
// prvky, jestli nahodou nejsou taky s danym klicem (u std::multimap)
std::cout << "Plzen: " << i->second << "\n";
// v tomto pripade nema zadnou extra vyhodu
std::cout << "Plzen: " << M["Plzen"] << "\n";
#ifdef MULTIMAP
// v std::multimap lze mit duplicitni klice a tak ma smysl hledat vsechny
std::cout << "\nhledam vsechny vyskyty Plzen" << std::endl;
// vyuzijeme funkci equal_range, ktera vraci par iteratoru
typedef std::multimap<std::string, int>::iterator mit;
std::pair<mit, mit> nalez = M.equal_range("Plzen");
// pocet nalezenych vysledku lze zjistit jako vzdalenost mezi
std::cout << "pocet nalezu: " << std::distance(nalez.first, nalez.second) << std::endl;
// vypis nalezu zaznamu s klicem Plzen
for (mit i = nalez.first; i != nalez.second; i++)
std::cout << std::left << std::setw(13) << i->first << " " << i->second << std::endl;
#endif
auto ii = M.find("Liberec");
// find vraci iterator, ktery ukazuje na prvek,
// pokud prvek neni nalezen, ma hodnotu end() -- prvni prvek za poslednim prvkem M
if (ii == M.end()) {
std::cout << "zaznam nenalezen\n";
}
else {
std::cout << "Liberec: " << ii->second << "\n";
}
std::cout << "Liberec: " << M["Liberec"] << "\n";
return 0;
}
\ No newline at end of file
/*
multimap - asociativni pole s duplicitnim klicem
*/
#include <iostream>
#include <map>
#include <cstdlib>
#include <ctime>
int main()
{
// inicializace nahodneho generatoru
std::srand(std::time(nullptr));
std::multimap <std::string, int> T;
for (int i = 0; i < 5; i++)
T.insert({"obyvak", std::rand() % 25});
for (int i = 0; i < 5; i++)
T.insert({"kuchyne", std::rand() % 22});
for (auto t : T)
std::cout << t.first << " - " << t.second << "\n";
// jak filtrovat hodnoty? bud find, ktera najde prvni vyskyt
auto i = T.find("obyvak");
std::cout << "find: obyvak\n";
while (i != T.end())
{
std::cout << i->first << " - " << i->second << std::endl;
i++;
}
// equal_range vraci par iteratoru
auto p = T.equal_range("kuchyne");
// std::multimap<std::string, int>::iterator it = p.first;
auto it = p.first;
std::cout << "find: kuchyn\n";
for (; it != p.second; it++)
std::cout << it->first << " - " << it->second << std::endl;
return 0;
}
\ No newline at end of file
/*
trideni vektoru vs. lambda funkce
*/
#include <iostream>
#include <vector>
#include <algorithm>
int main ()
{
// lambda funkce [] () {}
int y = 20;
auto f = [&] (int x) {return ++y*x;};
std::cout << f(3) << std::endl;
std::vector<int> A = {10, 30, 22, 11, 9, 5, 3, 0, 4};
for (int a : A)
std::cout << a << " ";
std::cout << std::endl;
std::sort(A.begin(), A.begin()+5);
for (int a : A)
std::cout << a << " ";
std::cout << std::endl;
/*
algoritmus std::sort predava lambda funkci hodnoty dvou po
sobe jdoucich prvku zdroje dat - kontejneru aa
*/
std::sort(A.begin(), A.end(), [](int a, int b){return a > b;});
for (int a : A)
std::cout << a << " ";
std::cout << std::endl;
std::vector<int> B;
B.resize(A.size());
std::copy (A.begin(), A.end(), B.begin());
for (int b : B)
std::cout << b << " ";
std::cout << std::endl;
/*
algoritmus std::copy_if predava lambda funkci hodnotu prvku
zdroje dat - kontejneru A; pokud je navratova hodnota lambdy
true, dojde ke zkopirovani do ciloveho kontejneru
*/
std::vector<int> C;
C.resize(A.size());
std::copy_if (A.begin(), A.end(), C.begin(), [](int a){return a > 10;});
for (int c : C)
std::cout << c << " ";
std::cout << std::endl;
return 0;
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment