Commit d77786a5 authored by Šmelko Adam Mgr.'s avatar Šmelko Adam Mgr.
Browse files

Add Cv4.

parent 42bfb08a
......@@ -26,6 +26,7 @@ Deadliny:
| 30.09.2021 | Úvod, nároky, syntaxe, I/O | [CV1](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv1.pdf) |
| 07.10.2021 | Stringy, znaky, streamy | [CV2](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv2.pdf) |
| 14.10.2021 | Hlavičky, třídy, objekty | [CV3](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv3.pdf) |
| 21.10.2021 | Kontejnery, iterátory | [CV4](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv4.pdf) |
## Úkoly:
......
#include <vector>
#include <string>
#include <memory>
struct CopyableStructure
{
std::vector<std::string> data;
};
struct NotCopyableStructure
{
std::unique_ptr<int> data;
};
int main()
{
{
CopyableStructure original;
CopyableStructure copy(original);
}
{
NotCopyableStructure original;
// does not compile because std::unique_ptr does not have copy constructor
// so copy constructor of NotCopyableStructure is not implicitly generated
NotCopyableStructure copy(original);
}
return 0;
}
\ No newline at end of file
#include <vector>
#include <string>
#include <iostream>
struct person
{
std::string name;
int age;
};
class person_catalog
{
std::vector<person> persons_;
public:
person_catalog(std::vector<person> persons)
: persons_(std::move(persons)) {}
struct iterator
{
person_catalog& owner;
int current_index;
iterator(person_catalog& owner, int starting_index)
: owner(owner), current_index(starting_index) {}
person& operator*()
{
return owner.persons_[current_index];
}
person* operator->()
{
return &owner.persons_[current_index];
}
bool operator!=(const iterator& oth) const
{
return &owner != &oth.owner || current_index != oth.current_index;
}
iterator& operator++()
{
current_index++;
return *this;
}
};
iterator begin()
{
return iterator(*this, 0);
}
iterator end()
{
return iterator(*this, persons_.size());
}
};
int main()
{
std::vector<person> persons;
persons.push_back(person{"Adam", 24});
persons.push_back(person{"Eva", 18});
persons.push_back(person{"Michal", 13});
person_catalog catalog(persons);
for (auto it = catalog.begin(); it != catalog.end(); ++it)
{
std::cout << it->name << " ";
}
std::cout << std::endl;
for (auto person : catalog)
std::cout << person.name << " ";
std::cout << std::endl;
}
\ No newline at end of file
Markdown is supported
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