MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Examples/Modules/Chapter 17/Ex17_04/Ex17_04.cpp:6–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import <string>;
5
6int main()
7{
8 std::string words[]{ "The", "quick", "brown", "fox", "jumps" };
9 Stack<std::string> wordStack; // A stack of strings
10
11 for (const auto& word : words)
12 wordStack.push(word);
13
14 Stack<std::string> newStack{ wordStack }; // Create a copy of the stack
15
16 // Display the words in reverse order
17 while (!newStack.isEmpty())
18 std::cout << newStack.pop() << ' ';
19 std::cout << std::endl;
20
21 // Reverse wordStack onto newStack
22 while (!wordStack.isEmpty())
23 newStack.push(wordStack.pop());
24
25 // Display the words in original order
26 while (!newStack.isEmpty())
27 std::cout << newStack.pop() << ' ';
28 std::cout << std::endl;
29
30 std::cout << std::endl << "Enter a line of text:" << std::endl;
31 std::string text;
32 std::getline(std::cin, text); // Read a line into the string object
33
34 Stack<const char> characters; // A stack for characters
35
36 for (size_t i{}; i < text.length(); ++i)
37 characters.push(text[i]); // Push the string characters onto the stack
38
39 std::cout << std::endl;
40 while (!characters.isEmpty())
41 std::cout << characters.pop(); // Pop the characters off the stack
42
43 std::cout << std::endl;
44}

Callers

nothing calls this directly

Calls 3

pushMethod · 0.45
isEmptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected