Skip to content

Commit

Permalink
refactor 11.11 and 11.12_13
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 15, 2014
1 parent 2ea0810 commit 0d66d06
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 83 deletions.
4 changes: 4 additions & 0 deletions ch11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ I use `set` when i just need to store the `key`, In other hand, I would like u

## [Exercise 11.7](ex11_7.cpp)
## [Exercise 11.8](ex11_8.cpp)
## [Exercise 11.9 and 11.10](ex11_9_10.cpp)
## [Exercise 11.11](ex11_11.cpp)
## [Exercise 11.12 and 11.13](ex11_12_13.cpp)
## [Exercise 11.14](ex11_14.cpp)
48 changes: 17 additions & 31 deletions ch11/ex11_11.cpp
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);
}
80 changes: 28 additions & 52 deletions ch11/ex11_12_13.cpp
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;
}

0 comments on commit 0d66d06

Please sign in to comment.