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.
following way?
std::find(argv, argv + argc, "-v")
Thanks.
Why can't I use the find algorithm to find a program argument in the
following way?
std::find(argv, argv + argc, "-v")
Thanks.
Why can't I use the find algorithm to find a program argument in the
following way?
std::find(argv, argv + argc, "-v")
Thanks.
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 ) );
}
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")
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".
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?
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]);
}
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.