473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Finding a program argument

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

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

Thanks.
Apr 9 '07 #1
7 1771
* 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::stringStringVector;

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 ) );
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 9 '07 #2
On Apr 9, 3:15 am, "Jason Heyes" <jasonhe...@optusnet.com.auwrote:
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::stringvs;
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
*/

Apr 9 '07 #3
Alf P. Steinbach wrote:
....
To avoid that, do the following:

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

typedef std::vector<std::stringStringVector;

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".
Apr 9 '07 #4
Jason Heyes wrote:
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)
Apr 9 '07 #5
* Gianni Mariani:
Alf P. Steinbach wrote:
...
>To avoid that, do the following:

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

typedef std::vector<std::stringStringVector;

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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 9 '07 #6
Alf P. Steinbach wrote:
* Gianni Mariani:
>Alf P. Steinbach wrote:
...
>>To avoid that, do the following:

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

typedef std::vector<std::stringStringVector;

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.
Apr 9 '07 #7
Salt_Peter <pj*****@yahoo.comwrote:
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::stringvs;
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::stringvs(argv, argv + argc);

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 11 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
2
by: Sara Khalatbari | last post by:
I'm writing a code that checks the header of .po file for syntax errors. I want this program to run msgfmt.py on the .po file first & then the rest. How can you write a code that runs another...
6
by: SSG | last post by:
Hai All! I need the optimized code for finding the string length. i dont want to use strlen function......... can anyone know reply........ By S.S.G
20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
26
by: Martin Jørgensen | last post by:
Hi, I'm learning C-programming. I have a program which I would like to modify so it takes arguments from the commandline. Let call the program: program.exe. Could somebody shortly explain how...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
5
by: tcwarrior | last post by:
Hi all. I am trying to write a little program where I extract some data out of a game in real time. The game writes some info to a log file, and I can access this in real time. The problem is that...
17
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.