Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Практика 10
#include <iostream>
#include <vector>
#include <algorithm>
class integer_set {
public:
using const_iterator = std::vector<int>::const_iterator;
integer_set() { }
integer_set(const std::vector<int>& numbers)
: container_(numbers)
{
std::sort(container_.begin(), container_.end());
std::vector<int>::iterator end = std::unique(container_.begin(), container_.end());
container_.resize(end - container_.begin());
container_.shrink_to_fit();
}
const std::vector<int>& container() const {
return container_;
}
bool has_element(int element) const {
return std::binary_search(container_.begin(), container_.end(), element);
}
integer_set operator& (const integer_set& rhs) const {
integer_set result;
std::set_intersection(container_.begin(), container_.end(),
rhs.container_.begin(), rhs.container_.end(),
std::back_inserter(result.container_));
return result;
}
integer_set operator| (const integer_set& rhs) const {
integer_set result;
std::set_union(container_.begin(), container_.end(),
rhs.container_.begin(), rhs.container_.end(),
std::back_inserter(result.container_));
return result;
}
const_iterator begin() const {
return container_.begin();
}
const_iterator end() const {
return container_.end();
}
private:
std::vector<int> container_;
};
int main() {
integer_set s1({1, 7, 9});
integer_set s2({3, 0});
integer_set s3({2, 3, 9});
for (const int& element : s2 | s1 & s3) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
#include <vector>
#include <iostream>
int main() {
std::vector<int> v(100 * 1000);
for (int i = 0; i < 50 * 1000; i++) {
v.pop_back();
}
std::vector<int>(v).swap(v);
std::cout << v.capacity() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.