Re: Finding a program argument
On Apr 9, 3:15 am, "Jason Heyes" <jasonhe...@optusnet.com.au> wrote:
Why can't I use the find algorithm to find a program argument in the
following way?
std::find(argv, argv + argc, "-v")
Thanks.
argv is an array of pointers (char*), not an array of strings
try loading the parameters in a container and then iterate through it.
A std::vector of std::string is perfect for the job.
int main(int argc, char* argv[])
{
std::vector<std::string> vs;
for(int n = 0; n < argc; ++n)
{
std::cout << "argument " << n;
std::cout << " is ";
std::cout << argv[n] << std::endl;
vs.push_back(argv[n]);
}
typedef std::vector<std::string>::iterator VIter;
VIter viter = std::find(vs.begin(), vs.end(), "-v");
if(viter != vs.end())
{
std::cout << "parameter -v found\n";
} else {
std::cout << "parameter not found\n";
}
return 0;
}
/*
argument 0 is ./proj_test
argument 1 is -a
argument 2 is -b
argument 3 is -v
parameter -v found
*/