Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <string>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- class AddressBook
- {
- public:
- // 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> _addresses = {
- "Alpha@networkone.com",
- "Bravo@networkone.com",
- "Charlie@networkone.com",
- "Delta@networkone.com",
- "Echo@networkone.com",
- "Foxtrot@networkone.com",
- "Golf@networkone.com",
- "Hotel@networktwo.com",
- "India@networktwo.com",
- "Juliet@networktwo.org",
- "Kilo@networktwo.com",
- "Lima@networktwo.com",
- "Mike@networktwo.com",
- "November@networktwo.com",
- "Oscar@networktwo.com",
- "Papa@networkthree.com",
- "Quebec@networkthree.com",
- "Romeo@networkthree.org",
- "Sierra@networkthree.com",
- "Tango@networkthree.com",
- "Uniform@networkthree.com",
- "Victor@networkthree.com",
- "Whiskey@networkthree.com",
- "X-ray@networkthree.com",
- "Yankee@networkthree.com",
- "Zulu@networkthree.com"};
- };
- AddressBook global_address_book;
- vector<string> findAddressesFromOrgs ()
- {
- auto lambda = [](std::string address) {
- std::size_t found = address.find(".org");
- if (found!=std::string::npos)
- return true;
- return false;
- /*
- krócej:
- return address.find(".org" != std::string::npos)*/
- };
- return global_address_book.findMatchingAddresses(lambda);
- /*
- 1. @todo
- Write a body of this function.
- Make it return a vector of strings which are all addresses
- from global_address_book that contain '.org',
- using findMatchingAddresses method
- */
- }
- vector<string> findAddressesFromName ()
- {
- std::string name;
- std::cin >> name;
- return global_address_book.findMatchingAddresses([&]
- (std::string address) {return address.find(name) != std::string::npos;});
- /*
- 2. @todo
- Write a body of this function.
- Read in name of a user from standard input.
- Make it return a vector of strings which are all addresses
- from global_address_book that contain the specified name,
- using findMatchingAddresses method.
- */
- }
- vector<string> findAddressesMinLen (int min_len)
- {
- return global_address_book.findMatchingAddresses([]
- (std::string address) {return address.length() >= min_len; });
- /*
- 3. @todo
- Write a body of this function.
- Make it return a vector of strings which are all addresses
- from global_address_book that are longer or equal to min_len,
- using findMatchingAddresses method.
- */
- }
- int main()
- {
- vector<string> v1 = findAddressesFromOrgs();
- vector<string> v2 = findAddressesFromName();
- vector<string> v3 = findAddressesMinLen(21);
- cout << "---v1---" << endl;
- for ( auto itr = v1.begin(), end =v1.end(); itr != end; ++itr )
- cout << *itr << endl;
- cout << "--v2---" << endl;
- for ( auto itr = v2.begin(), end =v2.end(); itr != end; ++itr )
- cout << *itr << endl;
- cout << "--v3---" << endl;
- for ( auto itr = v3.begin(), end =v3.end(); itr != end; ++itr )
- cout << *itr << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement