Design Pattern Examples
Overview of object-oriented design patterns
Iterator_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8
9#include "helpers/formatstring.h"
10
11#include "Iterator_Exercise.h"
12#include "Iterator_Class.h"
13
14
16{
17
27 // ! [Using Iterator in C++]
29 {
30 std::cout << std::endl;
31 std::cout << "Iterator Exercise" << std::endl;
32
33 // For this example, the class already has built into it the data
34 // to be iterated over.
36
37 std::cout << " Iterating over keys only:" << std::endl;
38 auto keyIterator = items.GetKeys();
39 std::string item;
40 while (keyIterator->Next(item))
41 {
42 std::cout << Helpers::formatstring(" %s", item.c_str()) << std::endl;
43 }
44
45 std::cout << " Iterating over values only:" << std::endl;
46 auto valueIterator = items.GetValues();
47 std::string value;
48 while (valueIterator->Next(value))
49 {
50 std::cout << Helpers::formatstring(" %s", value.c_str()) << std::endl;
51 }
52
53 std::cout << " Iterating over all items:" << std::endl;
54 auto itemIterator = items.GetItems();
55 ItemPair key_value_pair;
56 while (itemIterator->Next(key_value_pair))
57 {
58 std::cout << Helpers::formatstring(" %s = %s",
59 key_value_pair.Key.c_str(), key_value_pair.Value.c_str())
60 << std::endl;
61 }
62
63 std::cout << " Done." << std::endl;
64 }
65 // ! [Using Iterator in C++]
66
67} // end namespace
Declaration of the IteratorContainer_Class class, the IIterator interface, along with the implementat...
Represents a key/value pair where the key and value are strings.
Represents a container that offers up two kinds of iterators for the hardcoded contents,...
std::shared_ptr< IIterator< std::string > > GetKeys()
Retrieve an iterator over the "key" part of the data, where the data returned from the iterator is a ...
std::shared_ptr< IIterator< ItemPair > > GetItems()
Retrieve an iterator over the data that returns an ItemPair object containing both key and value for ...
std::shared_ptr< IIterator< std::string > > GetValues()
Retrieve an iterator over the "value" part of the data, where the data returned from the iterator is ...
Declaration of the Iterator_Exercise() function as used in the Iterator Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Iterator_Exercise()
Example of using the Iterator design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....