Created
November 14, 2023 05:59
-
-
Save mshafae/759d0d17b0bb03873516dcc9e16f5dca to your computer and use it in GitHub Desktop.
Create a vector of vectors, the inner vectors contain strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gist https://gist.github.com/mshafae/759d0d17b0bb03873516dcc9e16f5dca | |
// Filename cpsc120_2D_vector_pt2.cpp | |
// CompileCommand clang++ -std=c++17 cpsc120_2D_vector_pt2.cpp | |
// Create a vector of vectors, the inner vectors contain strings | |
// Note that C++ is row major order. | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
int main(int argc, char const *argv[]) { | |
std::vector<std::string> arguments{argv, argv + argc}; | |
// Vectors are row major order like a movie theater. This means the first | |
// index is the row and the second index is the column (or seat). All the | |
// types must be the same in an vector. This is a declaration for a 2 x 3 | |
// vector - 2 rows, 3 columns. Notice the double braces. | |
// The table below looks like this: | |
// | "Pastrami on Rye" | "YES" | | |
// | "Hot Dog" | "YES" | | |
// | "Burrito" | "NO" | | |
std::vector<std::vector<std::string>> sandwiches{ | |
{"Pastrami on Rye", "YES"}, | |
{"Hot Dog", "YES"}, | |
{"Burrito", "NO"} | |
}; | |
if (arguments.size() < 2) { | |
std::cout << "Please provide a sandwich name on the command line.\n"; | |
std::cout << "For example: ./a.out \"Pastrami on Rye\"\n"; | |
std::cout << "Exiting.\n"; | |
return 1; | |
} | |
bool index{0}; | |
bool was_found{false}; | |
bool is_sandwich{false}; | |
// Search for the sandwich passed on the command line | |
// auto is replaced with std::vector<std::string> by the compiler | |
for (const auto& element : sandwiches) { | |
// Each element is a pair. Location 0 has the sandwich name and locaiton 1 | |
// has the Yes or No. | |
if (element.at(0) == arguments.at(1)) { | |
was_found = true; | |
is_sandwich = (element.at(1) == "YES"); | |
break; | |
} | |
index = index + 1; | |
} | |
if (was_found && is_sandwich) { | |
std::cout << sandwiches.at(index).at(0) << " is considered a sandwich.\n"; | |
} else if (was_found && !is_sandwich) { | |
std::cout << sandwiches.at(index).at(0) | |
<< " is NOT considered a sandwich.\n"; | |
} else { | |
std::cout << "Sorry, I don't know about \"" << arguments.at(1) << "\"\n"; | |
std::cout << "I can't tell you if it is a sandwich or not.\n"; | |
std::cout << "Exiting.\n"; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment