class couple {
friend std::ostream& operator<<(std::ostream& os, const couple& c);
public:
couple();
couple(const std::string& one, const std::string& two);
const std::string& getFirst() const;
const std::string& getSecond() const;
private:
std::string first;
std::string second;
};
couple weddingParty[4];
map<string, couple> addressBook;
couple::couple() {}
couple::couple(const string& one, const string& two) : first(one), second(two) {}
const string& couple::getFirst() const { return first; }
const string& couple::getSecond() const { return second; }
ostream& operator<<(ostream& os, const couple& c) { // note this is a function!!!
os << c.first << " and " << c.second;
return os;
}int main(int argc, char *argv[]) {
couple hosts("Colleen", "Joe");
assert(hosts.getFirst() == "Colleen");
assert(hosts.getSecond() == "Joe");
couple guests[kNumCouples]; // works only because we provide a zero-argument constructor
assert(guests[0].getFirst().empty());
guests[0] = couple("Robin", "Jamie");
guests[1] = couple("April", "Chris");
guests[2] = couple("Jennifer", "Andrew");
cout << "Dinner party guest list: " << endl;
cout << "Hosts: " << hosts << endl;
for (const couple& c: guests) { // C++11 for loop works because context provides array length
cout << "Guests: " << c << endl;
}
cout << endl;
cout << "We're serving duck!" << endl;
return 0;
} myth22> ./dinner-party
Dinner party guest list:
Hosts: Colleen and Joe
Guests: Robin and Jamie
Guests: April and Chris
Guests: Jennifer and Andrew
We're serving duck!
myth22>
template <typename Iterator, typename T>
Iterator find(Iterator begin, Iterator end, const T& elem) {
Iterator curr = begin;
while (curr != end && *curr != elem) {
++curr;
}
return curr;
}
int main(int argc, char *argv[]) {
int numbers[] = {3, 1, 5, 8, 9, 5};
assert(find(numbers, numbers + 6, 3) == numbers);
assert(find(numbers, numbers + 6, 9) == numbers + 4);
assert(find(numbers, numbers + 6, 5) == numbers + 2);
assert(find(numbers, numbers + 6, 7) == numbers + 6);
assert(find(numbers + 1, numbers + 3, 3) == numbers + 3);
double gpas[] = {3.5, 3.1, 3.6, 3.2, 1.7, 3.0, 2.8, 3.1, 3.22};
assert(find(gpas, gpas + 9, 3.6) == gpas + 2);
assert(find(gpas, gpas + 9, 3.22) == gpas + 8);
assert(find(gpas, gpas + 9, 1.1) == gpas + 9);
assert(find(gpas + 4, gpas + 5, 1.7) == gpas + 4);
string friends[] = {"Scott", "Jon", "Arlene", "David", "Tim", "Shana", "James", "Lisa"};
assert(find(friends, friends + 8, "Shana") == friends + 5);
cout << "Made it to the end without asserting." << endl;
return 0;
}
static bool isEven(int number) { return number % 2 == 0; }
static bool isGreaterThan(int one, int two) { return one > two; }
static void assertIsOdd(int number) { assert(!isEven(number)); }
int main(int argc, char *argv[]) {
int numbers[] = {11, 7, 19, 3, 17, 5, 2, 13};
int *found = find(numbers, numbers + 8, 17);
assert(found == numbers + 4 && *found == 17);
found = find_if(numbers, numbers + 8, isEven);
assert(found == numbers + 6 && *found == 2);
assert(accumulate(numbers, numbers + 8, 0) == 77);
sort(numbers, numbers + 8);
assert(is_sorted(numbers, numbers + 8));
assert(binary_search(numbers, numbers + 8, 11));
assert(!binary_search(numbers, numbers + 8, 12));
for_each(numbers + 1, numbers + 8, assertIsOdd);
found = find(numbers, numbers + 8, 19);
assert(found == numbers + 7 && *found == 19);
found = lower_bound(numbers, numbers + 8, 17);
assert(found == numbers + 6 && *found == 17);
found = lower_bound(numbers, numbers + 8, 14);
assert(found == numbers + 6 && *found == 17);
reverse(numbers, numbers + 8);
assert(is_sorted(numbers, numbers + 8, isGreaterThan));
cout << "Made it to the end without asserting." << endl;
return 0;
} static const int kNumbers[] = {2, 3, 5, 7, 11, 13, 17, 21, 23, 27, 29, 31};
static const int kCount = sizeof(kNumbers)/sizeof(kNumbers[0]);
int main(int argc, char *argv[]) {
vector<int> primes(kNumbers, kNumbers + kCount);
primes.push_back(37);
primes.push_back(41);
vector<int>::const_iterator curr = primes.cbegin();
assert(*curr == 2);
++curr; // prefix form is standard in STL-oriented C++
assert(*curr == 3);
assert(is_sorted(primes.cbegin(), primes.cend()));
// linear search
assert(find(primes.cbegin(), primes.cend(), 14) == primes.cend());
assert(find(primes.cbegin(), primes.cend(), 7) != primes.cend());
assert(find(primes.cbegin(), primes.cend(), 7) == primes.cbegin() + 3);
// binary_search
assert(binary_search(primes.cbegin(), primes.cend(), 41));
assert(!binary_search(primes.cbegin(), primes.cend(), 33));
assert(!binary_search(primes.cbegin() + 1, primes.cend() - 1, 41));
auto found = lower_bound(primes.begin(), primes.end(), 19);
assert(found == primes.begin() + 7 && *found == 21); // insert that 19 before the 21 to maintain sorted order
auto iter = primes.insert(found, 19); // return value addresses the item just inserted
assert(*iter == 19);
primes.erase(iter + 1); // reach one beyond the 19 to the 21 and delete it
assert(is_sorted(primes.cbegin(), primes.cend()));
assert(binary_search(primes.cbegin(), primes.cend(), 19));
assert(!binary_search(primes.cbegin(), primes.cend(), 21)); printPrimesOne(primes);
printPrimesTwo(primes);
printPrimesThree(primes);
cout << endl;
cout << "Made it to the end without asserting." << endl;
return 0;
}static void printPrimesOne(const vector<int>& v) {
cout << "Vector contents (take 1): " << endl << endl;
for (size_t i = 0; i < v.size(); i++) {
cout << " " << setw(2) << (i + 1) << ".) " << v[i] << endl;
}
cout << endl;
} static void printPrimesTwo(const vector<int>& v) {
cout << "Vector contents (take 2): " << endl << endl;
size_t label = 1;
for (int prime: v) {
cout << " " << setw(2) << label << ".) " << prime << endl;
label++;
}
cout << endl;
}static void printPrimesThree(const vector<int>& v) {
cout << "Vector contents (take 3): " << endl << endl;
size_t label = 1;
for_each(v.begin(), v.end(), [&label](int prime) -> void {
cout << " " << setw(2) << label << ".) " << prime << endl;
label++;
});
}
int main(int argc, char *argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <text-file>" << endl;
return kWrongArgumentCount;
}
ifstream infile(argv[1]);
if (infile.fail()) {
cerr << "File named \"" << argv[1] << "\" couldn't be opened." << endl;
return kFileNotAccessible;
}
map<string, size_t> m;
StreamTokenizer st(infile, kDelimiters);
while (st.hasMoreTokens()) {
string str = toLowerCase(st.nextToken());
m[str]++;
}
cout << "Ingested all of \"" << argv[1] << "\"." << endl;
cout << "Discovered " << m.size() << " unique words." << endl;
vector<pair<string, size_t>> alphabeticallySortedWords(m.cbegin(), m.cend());
printLeadingWords("Leading words, sorted alphabetically", alphabeticallySortedWords, 15);
vector<pair<string, size_t>> frequentlyOccurringWords(alphabeticallySortedWords);
auto compfn = [](const pair<string, size_t>& one, const pair<string, size_t>& two) -> bool {
return
(one.second > two.second) ||
(one.second == two.second && one.first < two.first);
};
sort(frequentlyOccurringWords.begin(), frequentlyOccurringWords.end(), compfn);
printLeadingWords("Leading words, sorted by frequency", frequentlyOccurringWords, 15);
while (true) {
cout << "Enter a word [or just hit enter to quit]: ";
string word;
getline(cin, word);
word = toLowerCase(trim(word));
if (word.empty()) break;
auto found = m.find(word);
if (found == m.end()) {
cout << "The word \"" << word << "\" isn't present. #facepalm" << endl;
} else {
cout << "The word \"" << word << "\" is present and appears this many times: "
<< found->second << " [with frequency rank: ";
const pair<string, size_t>& match = *found;
auto found = lower_bound(frequentlyOccurringWords.cbegin(),
frequentlyOccurringWords.cend(), match, compfn);
size_t rank = found - frequentlyOccurringWords.cbegin() + 1;
cout << rank << "]" << endl;
}
}
return 0;
}
static void printLeadingWords(const string& header,
const vector<pair<string, size_t>>& words, size_t count) {
cout << header << ":" << endl;
cout << endl;
auto curr = words.cbegin();
for (size_t i = 1; i <= count && curr != words.cend(); i++, ++curr) {
cout << " " << setw(2) << i << ".) " << curr->first
<< " [count: " << curr->second << "]" << endl;
}
cout << endl;
}