Created
November 24, 2019 13:59
-
-
Save vasalf/0a32a13c9e299444ca5ae56334c0247e to your computer and use it in GitHub Desktop.
Практика 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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