Finding a program argument

J

Jason Heyes

Why can't I use the find algorithm to find a program argument in the
following way?

std::find(argv, argv + argc, "-v")

Thanks.
 
A

Alf P. Steinbach

* Jason Heyes:
Why can't I use the find algorithm to find a program argument in the
following way?

std::find(argv, argv + argc, "-v")

Thanks.

Because you're comparing pointer values.

To avoid that, do the following:

#include <algorithm>
#include <string>
#include <vector>

typedef std::vector<std::string> StringVector;

void cppMain( StringVector const& args )
{
if( std::find( args.begin(), args.end(), "-v" ) != args.end() )
{
// ...
}
}

int main( int n, char* a[] )
{
cppMain( StringVector( a, a + n ) );
}
 
S

Salt_Peter

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
*/
 
G

Gianni Mariani

Alf P. Steinbach wrote:
....
To avoid that, do the following:

#include <algorithm>
#include <string>
#include <vector>

typedef std::vector<std::string> StringVector;

void cppMain( StringVector const& args )
{
if( std::find( args.begin(), args.end(), "-v" ) != args.end() )
{
// ...
}
}

int main( int n, char* a[] )
{
cppMain( StringVector( a, a + n ) );
}

That technique does not work so well if you have an argument value that
is "-v".
 
P

Pete Becker

Jason said:
Why can't I use the find algorithm to find a program argument in the
following way?

std::find(argv, argv + argc, "-v")

You're dealing here with char*'s, and by default std::find compares the
addresses that they hold. Since you need to compare the C-style strings
that they point to, you have to provide a predicate that does that, and
use find_if:

struct cmp
{
cmp(const char *str) : tgt(str) {}
const char *tgt;
bool operator()(const char *s)
{
return strcmp(tgt, s) == 0;
}
};

std::find_if(argv, argv + argc, cmp("-v"));

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
A

Alf P. Steinbach

* Gianni Mariani:
Alf P. Steinbach wrote:
...
To avoid that, do the following:

#include <algorithm>
#include <string>
#include <vector>

typedef std::vector<std::string> StringVector;

void cppMain( StringVector const& args )
{
if( std::find( args.begin(), args.end(), "-v" ) != args.end() )
{
// ...
}
}

int main( int n, char* a[] )
{
cppMain( StringVector( a, a + n ) );
}

That technique does not work so well if you have an argument value that
is "-v".

What do you mean?
 
F

Fei Liu

Alf said:
* Gianni Mariani:
Alf P. Steinbach wrote:
...
To avoid that, do the following:

#include <algorithm>
#include <string>
#include <vector>

typedef std::vector<std::string> StringVector;

void cppMain( StringVector const& args )
{
if( std::find( args.begin(), args.end(), "-v" ) != args.end() )
{
// ...
}
}

int main( int n, char* a[] )
{
cppMain( StringVector( a, a + n ) );
}

That technique does not work so well if you have an argument value
that is "-v".

What do you mean?

I think he's trying to say something like this
prog -option -v where '-v' is argument value to an argument option.
 
M

Marcus Kwok

Salt_Peter said:
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]);
}

Personally, I would initialize the string vector instead of building it
incrementally:

std::vector<std::string> vs(argv, argv + argc);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top