Lets say you're a purist object oriented programmer and you want wrap the argc
and argv
constant variables in a more situable
container like std::vector
and don't know how?
Here is the pre-canned solution just for you (licensed under the WTFPL):
// File: ArgumentsList.hpp
typedef std::vector<std::string> ArgumentsList;
void fill_arguments(ArgumentsList &args, int argc, const char **argv)
{
for (int i = 0; i < argc; i++)
args[i] = std::string(argv[i]);
}
And use it like this:
#include "ArgumentsList.hpp"
void print_arguments(const ArgumentsList &arguments)
{
for (int i = 0; i < arguments.size(); i++)
std::cout << "arguments[" << i << "]: " << arguments[i] << std::endl;
}
int main(int argc, const char **argv)
{
ArgumentsList arguments(argc);
fill_arguments(arguments, argc, argv);
print_arguments(arguments);
return 0;
}