forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
83 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,23 @@ | ||
//! @Alan | ||
//! Exercise 11.11: | ||
//! Redefine bookstore without using decltype. | ||
// discussion on stack overflow: | ||
// http://stackoverflow.com/questions/20608365/is-it-possible-to-code-this-waywhilelambda | ||
//! | ||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
#include <algorithm> | ||
#include <list> | ||
// | ||
// ex11_11.cpp | ||
// Exercise 11.11 | ||
// | ||
// Created by pezy on 12/15/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Redefine bookstore without using decltype. | ||
|
||
#include "../ch07/ex7_26.h" | ||
#include <set> | ||
|
||
|
||
|
||
class A | ||
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) | ||
{ | ||
int lenth; | ||
public: | ||
int getLenth() const {return lenth;} | ||
}; | ||
|
||
bool compareA(const A &a1, const A &a2) | ||
{ | ||
return a1.getLenth() < a2.getLenth(); | ||
return lhs.isbn() < rhs.isbn(); | ||
} | ||
|
||
int main() | ||
{ | ||
//! more approaches can be found on the post of SO. | ||
bool (*fp) (const A &a1, const A &a2) = compareA; | ||
|
||
std::multiset<A, bool (*) (const A &, const A &)> m1(fp); | ||
|
||
return 0; | ||
} | ||
|
||
|
||
using compareType = bool (*)(const Sales_data &lhs, const Sales_data &rhs); | ||
//typedef bool(*compareType)(const Sales_data &lhs, const Sales_data &rhs); | ||
std::multiset<Sales_data, compareType> bookstore(compareIsbn); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,33 @@ | ||
//! @Alan | ||
//! Exercise 11.12: | ||
//! Write a program to read a sequence of strings and ints, storing | ||
//! each into a pair. Store the pairs in a vector. | ||
//! | ||
//! Exercise 11.13: | ||
//! There are at least three ways to create the pairs in the program | ||
//! for the previous exercise. Write three versions of that program, | ||
//! creating the pairs in each way. Explain which form you think is | ||
//! easiest to write and understand, and why. | ||
//! | ||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
#include <algorithm> | ||
#include <list> | ||
#include <set> | ||
// | ||
// ex11_12_13.cpp | ||
// Exercise 11.12 11.13 | ||
// | ||
// Created by pezy on 12/15/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Write a program to read a sequence of strings and ints, | ||
// storing each into a pair. Store the pairs in a vector. | ||
// | ||
// There are at least three ways to create the pairs in the program for the previous exercise. | ||
// Write three versions of that program, creating the pairs in each way. | ||
// Explain which form you think is easiest to write and understand, and why. | ||
|
||
#include <vector> | ||
#include <utility> | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::vector<std::pair<std::string, int>> vec; | ||
std::string str; | ||
int i; | ||
std::string word; | ||
|
||
std::pair<std::string, int> pair; | ||
std::vector<std::pair<std::string, int>> v; | ||
|
||
while([&]() | ||
{ | ||
std::cout << "enter a word:\n"; | ||
std::cin >> word; | ||
std::cout << "enter an int:\n"; | ||
std::cin >> i; | ||
|
||
return word != "@q"; | ||
}()) | ||
{ | ||
//! way 1: | ||
//pair = {word, i}; | ||
//!^^^^^^^^^^^^^^^^^^^ | ||
//! way one is the easiest to write and understand. | ||
//! way 2: | ||
//pair = std::make_pair(word,i); | ||
//! way 3: | ||
pair = std::pair<std::string, int>(word,i); | ||
|
||
v.push_back(pair); | ||
for(auto e : v) | ||
std::cout << e.first << " " | ||
<< e.second << " "; | ||
std::cout << "\n"; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
while (std::cin >> str >> i) | ||
vec.push_back(std::pair<std::string, int>(str, i)); | ||
//vec.push_back(std::make_pair(str, i)); | ||
//vec.push_back({str, i}); | ||
//vec.emplace_back(str, i); //!!! easiest way. | ||
|
||
for (const auto &p : vec) | ||
std::cout << p.first << ":" << p.second << std::endl; | ||
} |