Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <string>
- class AddressBook {
- public:
- AddressBook() = default;
- AddressBook(std::initializer_list<std::string> list) : _addresses(list.begin(), list.end()) {}
- // using a template allows us to ignore the differences between functors, function pointers and lambda
- template<typename Func>
- std::vector<std::string> findMatchingAddresses(Func func) {
- std::vector<std::string> results;
- for (auto itr = _addresses.begin(), end = _addresses.end(); itr != end; ++itr) {
- // call the function passed into findMatchingAddresses and see if it matches
- if (func(*itr)) {
- results.push_back(*itr);
- }
- }
- return results;
- }
- std::vector<std::string> findAddressesFromOrgs_1() {
- return findMatchingAddresses(
- [](const std::string &addr) { return addr.find("noodles") != std::string::npos; }
- );
- }
- std::vector<std::string> findUserString() {
- std::string name;
- std::getline(std::cin, name);
- return findMatchingAddresses(
- [&](const std::string &addr) { return addr.find(name) != std::string::npos; }
- );
- }
- void insert(std::string &str) { _addresses.push_back(str); }
- void insertMoveIL(std::initializer_list<std::string> args) {
- std::copy(args.begin(), args.end(), std::back_inserter(_addresses));
- }
- private:
- std::vector<std::string> _addresses;
- friend std::ostream &operator<<(std::ostream &os, const AddressBook &addressBook) {
- for (const auto &x : addressBook._addresses) {
- std::cout << x << ' ';
- }
- std::cout << '\n';
- }
- };
- struct CakeType {
- size_t _weight;
- size_t _value;
- explicit CakeType(size_t weight = 0, size_t value = 0) : _weight(weight), _value(value) {}
- };
- struct X {
- int _x;
- explicit X(int x) : _x(x) {}
- };
- struct Z {
- int _z;
- explicit Z(int z) : _z(z) {}
- int operator()(int y) { return _z + y; }
- };
- struct Y : X {
- int _y;
- explicit Y(int x, int y = 0) : X{x}, _y(y) {}
- };
- struct B {
- void f(double);
- };
- struct D : B {
- using B::f; // bring all f()s from B into scope
- void f(int); // add a new f()
- };
- /* within main()...
- * B b; b.f(4.5); // fine
- * D d; d.f(4.5); // calls D::f(double) which is B::f(double)
- * // without using in using B::f in D, surprise: calls f(int) with argument 4
- */
- // Suffix return type syntax
- template<typename T, typename U>
- auto mult(T x, U y) -> decltype(x * y) { return x * y; }
- int main() {
- constexpr long long longx = 9223372036854775807LL;
- std::cout << std::is_literal_type<long>::value << std::endl;
- X x(3);
- std::cout << x._x << std::endl;
- Z z(5);
- std::cout << z(6) << std::endl; // 11
- std::cout << mult(6, 7) << std::endl;
- AddressBook bk{"2", "33"};
- bk.insertMoveIL({"2", "1", "0", "5"});
- std::string name("happy");
- bk.insert(name);
- std::cout << bk;
- auto results = bk.findUserString();
- for (const auto &xresults : results) std::cout << xresults << ' ';
- }
- // std::cout << std::is_literal_type<long>::value << std::endl;
- // Understanding the power of structs. See type_traits in stl for std::is_literal_type.
- // I deduced how to write this by checking stl implementation.
- // std is namespace, i_literal_type is struct <long> is _Tp template parameter for
- // base struct integral_constant which is bool. value is typedef its template parameter _Tp __v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement