Skip to content

Instantly share code, notes, and snippets.

@drewxa
Created November 7, 2019 08:34
Show Gist options
  • Save drewxa/5ddd095c04cb86de01cf0b3cc87aabbb to your computer and use it in GitHub Desktop.
Save drewxa/5ddd095c04cb86de01cf0b3cc87aabbb to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using Compare = std::function<bool(int, int)>;
using IT = std::vector<int>::iterator;
void my_sort(IT beg, IT end, Compare cmp) {
for (auto i = beg; i != end; ++i) {
for (auto j = beg; j != end; ++j) {
if (cmp(*i, *j)) {
std::iter_swap(i, j);
}
}
}
}
std::ostream& operator<<(std::ostream& out, const std::vector<int>& v) {
for (auto i : v) {
out << i << " ";
}
out << std::endl;
return out;
}
bool comp(int a, int b) {
return a < b;
}
bool cond(int a) {
return a < 0;
}
bool is_oven(int a) {
return a & 1;
}
bool is_even(int a) {
return !is_oven(a);
}
int pow3(int a) {
return a * a * a;
}
int main() {
std::vector<int> v = {-1, 0, 4, 2, -4, 0};
auto end = v.begin() + 3;
auto it = std::find(v.begin(), end, 0);
if (it != end) {
std::cout << "2 was found!" << std::endl;
std::cout << "index = " << std::distance(v.begin(), it) << std::endl;
} else {
std::cout << "2 was not found!" << std::endl;
}
auto it2 = std::find_if(v.begin(), v.end(), cond);
it2 = std::find_if(v.begin(), v.end(), [](int a) { return a > 0; });
if (it2 != v.end()) {
std::cout << *it2 << std::endl;
}
bool has_even = std::any_of(v.begin(), v.end(), is_even);
std::vector<int> out(v.size());
std::cout << v;
int num = 0;
std::cin >> num;
std::string s = "Asdfsdaf";
std::transform(v.begin(), v.end(), out.begin(),
[num](int a) { return a + num; });
std::cout << out;
std::cout << v;
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
std::cout << v;
std::vector<std::string> v2 = {"aaaa", "bb", "c"};
std::sort(v2.begin(), v2.end(),
[](const std::string& a, const std::string& b) {
return a.size() < b.size();
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment