Lists
This page is under construction. Please come back later.
#include "Utils.hpp" #include <fmt/format.h> #include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, char **argv) { int const NUM_SQUARES = 5; vector<int> squares = {}; // Put the squares into the list for (auto i = 0; i < NUM_SQUARES; ++i) { Utils::push(squares, i * i); } // Print out the squares from the list for (auto i = 0; i < squares.size(); ++i) { cout << fmt::format("{0:d}^2 = {1:d}", i, squares[i]) << endl; } cout << Utils::to_string(squares) << endl; return 0; }
Output
$ g++ -std=c++17 Lists1.cpp -o Lists1 -lfmt
$ ./Lists1
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
[0, 1, 4, 9, 16]
#include <fmt/format.h> #include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, char **argv) { vector<int> squares = {0, 1, 4, 9, 16, 25}; // Print out the squares from the list for (int i = 0; i < squares.size(); ++i) { cout << fmt::format("{0:d}^2 = {1:d}", i, squares[i]) << endl; } return 0; }
Output
$ g++ -std=c++17 Lists2.cpp -o Lists2 -lfmt
$ ./Lists2
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
#include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, char **argv) { vector<string> names = {"Fred", "Wilma", "Barney", "Betty"}; for (auto name : names) { cout << "Hello, " << name << "!" << endl; } names = {"Harry", "Ron", "Hermione"}; for (auto name : names) { cout << "Hello, " << name << "!" << endl; } return 0; }
Output
$ g++ -std=c++17 Lists3.cpp -o Lists3 -lfmt
$ ./Lists3
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Harry!
Hello, Ron!
Hello, Hermione!
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); // Print out the command line arguments for (int i = 1; i <= (argc - 1); ++i) { wcout << i << L":" << Utils::UTF8_to_wstring(argv[i]) << endl; } return 0; }
Output
$ g++ -std=c++17 Lists4.cpp -o Lists4 -lfmt
$ ./Lists4 Fred Barney Wilma Betty
1:Fred
2:Barney
3:Wilma
4:Betty
$ g++ -std=c++17 Lists4.cpp -o Lists4 -lfmt
$ ./Lists4 10 20 30 40
1:10
2:20
3:30
4:40
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <string> #include <vector> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); vector<wstring> names = {L"Fred", L"Wilma", L"Barney", L"Betty"}; // Print out the name based on command line argument 1-4 for (int i = 1; i <= (argc - 1); ++i) { int const x = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[i]), 0); wcout << i << L":" << names[x - 1] << endl; } return 0; }
Output
$ g++ -std=c++17 Lists5.cpp -o Lists5 -lfmt
$ ./Lists5 1 2 3 4
1:Fred
2:Wilma
3:Barney
4:Betty
$ g++ -std=c++17 Lists5.cpp -o Lists5 -lfmt
$ ./Lists5 4 3 2 1
1:Betty
2:Barney
3:Wilma
4:Fred
$ g++ -std=c++17 Lists5.cpp -o Lists5 -lfmt
$ ./Lists5 1 3
1:Fred
2:Barney
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]