Last active
January 18, 2016 11:41
-
-
Save ybouhjira/e0cfe9a0ed663a7c08d4 to your computer and use it in GitHub Desktop.
STL examples
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> | |
#include <ctime> | |
#include <cstdlib> | |
#include <iterator> | |
#include <functional> | |
#include <vector> | |
#include <list> | |
namespace sort_example { | |
class Foo { | |
int m_x; | |
public: | |
Foo(int x) : m_x(x) {} | |
bool operator< (const Foo &that) const { return m_x < that.m_x; } | |
int x() const { return m_x; } | |
}; | |
class Bar { | |
int m_x; | |
public: | |
Bar(int x) : m_x(x) {} | |
int x() const { return m_x; } | |
}; | |
bool bar_comparator(const Bar &a, const Bar &b) { | |
return a.x() < b.x(); | |
} | |
void compare_bar() { | |
std::vector<Bar> v = {1, 2, -3, 5, 10, -1, 6}; | |
std::sort(v.begin(), v.end(), bar_comparator); | |
// output | |
for (auto x : v) | |
std::cout << x.x() << " " ; | |
std::cout << std::endl; | |
} | |
void compare_foo() { | |
std::vector<Foo> v = {1, 2, -3, 5, 10, -1, 6}; | |
std::sort(v.begin(), v.end()); // no need for Compare function | |
// output | |
for (auto x : v) | |
std::cout << x.x() << " " ; | |
std::cout << std::endl; | |
} | |
} | |
namespace string_sort_example { | |
class Foo { | |
std::string m_str; | |
public: | |
Foo(const std::string &str) : m_str(str) {} | |
bool operator< (const Foo &other) const { return m_str < other.m_str; } | |
std::string str() const { return m_str; } | |
bool operator==(const Foo& o) { return m_str == o.str(); } | |
}; | |
std::ostream& operator<<(std::ostream& out, const string_sort_example::Foo &f) { | |
return out << f.str(); | |
} | |
void sort_foo() { | |
std::vector<Foo> v = { | |
(std::string)"Hello", (std::string)"World"}; | |
std::sort(v.begin(), v.end()); | |
for (auto x : v) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
} | |
void generate_n_example() { | |
std::vector<string_sort_example::Foo> foos; | |
std::generate_n(std::back_inserter(foos), 10, []() { | |
std::string str; | |
std::generate_n(std::back_inserter(str), 10, []() { | |
return (char) ((rand() % ('z' - 'a')) + 'a'); | |
}); | |
return str; | |
}); | |
auto contains_c = [](const Foo &foo) { | |
return foo.str().find('c') == std::string::npos; | |
}; | |
std::vector<Foo> foosContainingC; | |
std::copy_if(foos.begin(), | |
foos.end(), | |
std::back_inserter(foosContainingC), | |
contains_c); | |
std::cout << "Foos containing c : "; | |
for (auto x : foosContainingC) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
} | |
void find_example() { | |
std::cout << "Using std::find : \n"; | |
std::vector<Foo> v = { | |
(std::string) "one", | |
(std::string) "two", | |
(std::string) "three", | |
(std::string) "four", | |
}; | |
auto it = std::find(v.begin(), v.end(), Foo("two")); | |
while(it != std::end(v)) { | |
std::cout << *it << " "; | |
it ++; | |
} | |
std::cout << "\n\n" <<std::endl; | |
} | |
} | |
namespace vector_of_int { | |
void run() { | |
using namespace std::placeholders; | |
std::srand(time(nullptr)); | |
std::vector<int> v(10); | |
auto random_number = []() { return rand() % 100; }; | |
std::generate(v.begin(), v.end(), random_number); | |
std::ostream_iterator<int> couter(std::cout, " "); | |
std::copy(v.begin(), v.end(), couter); | |
std::cout << std::endl; | |
std::cout << std::endl; | |
std::random_shuffle(v.begin(), v.end()); | |
std::cout << "\nshuffled : \n"; | |
for (auto x : v) | |
std::cout << x << " " ; | |
std::cout << std::endl; | |
std::sort(v.begin(), v.end()); | |
std::cout << "\nsorted : \n"; | |
for (auto x : v) | |
std::cout << x << " " ; | |
std::cout << std::endl; | |
std::for_each(v.begin(), | |
v.end(), | |
[](int x) { std::cout << x << " "; }); | |
std::cout << std::endl; | |
std::cout << "\n\nnumber of 4s : " | |
<< std::count(v.begin(), v.end(), 4) | |
<< std::endl; | |
std::cout << "\n\neven numbers : "; | |
std::vector<int> evens; | |
std::copy_if(v.begin(), | |
v.end(), | |
std::back_inserter(evens), | |
[](int x) { return x % 2 == 0; }); | |
for (auto x : evens) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
} | |
} | |
namespace list_example { | |
void run() { | |
std::list<float> list; | |
list.push_front(1); | |
list.push_back(2); | |
list.push_back(3); | |
list.push_front(0); | |
auto it = std::find(list.begin(), list.end(), 2); | |
it++; | |
list.insert(it, 2.5); | |
for (auto i = 0; i < 10; i++) | |
list.push_back(i); | |
auto begin = std::find(list.begin(), list.end(), 3); | |
auto end = std::find(list.begin(), list.end(), 7); | |
list.erase(begin, end); | |
list.sort(); | |
auto target = 2.5; | |
auto found = std::binary_search(list.begin(), list.end(), target); | |
std::cout << "found " << target << ": " | |
<< (found? "yes" : "no") << std::endl; | |
list.remove_if([](float x) { return ((int)x) % 2 == 0; }); | |
for (auto x : list) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
} | |
} | |
int main() | |
{ | |
// string_sort_example::find_example(); | |
// string_sort_example::generate_n_example(); | |
// vector_of_int::run(); | |
// sort_example::compare_bar(); | |
// sort_example::compare_foo(); | |
list_example::run(); | |
srand(time(nullptr)); | |
std::vector<int> v = {1,2,3,4,5,6,7,8,9}; | |
for (auto x : v) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
std::remove(v.begin(), v.end(), 2); | |
for (auto x : v) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment