Skip to content

Instantly share code, notes, and snippets.

@BoolPurist
Last active March 9, 2022 21:43
Show Gist options
  • Save BoolPurist/a90f458025a1119808e3b90cbbca1879 to your computer and use it in GitHub Desktop.
Save BoolPurist/a90f458025a1119808e3b90cbbca1879 to your computer and use it in GitHub Desktop.
Small function to get a practical vector for the in-line arguments in a console. Otherwise one has to work with them via pointer arithmetic.
#include <string>
#include <vector>
/// Precondition: argc and argv were given as parameter for main function
/// because the represents the in lines arguments for a program.
/// Precondition: argc is the number of elements in argv.
/// Note: Returns an empty vector if argc is zero.
std::vector<std::string> makeArgsHandy(int argc, char** argv)
{
size_t size{static_cast<size_t>(argc)};
std::vector<std::string> converted{size};
if (size != 0)
{
converted.assign(argv, argv + argc);
}
return converted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment