Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   A vector of strings (http://www.velocityreviews.com/forums/t622478-a-vector-of-strings.html)

amphetaman@gmail.com 06-27-2008 01:21 AM

A vector of strings
 
Hello. I am writing code to put the traditional C-style command-line
arguments into a std::vector of std::strings. I was wondering: which
is the best way to do it? Are they identical?

#include <string>
#include <vector>

int mymain(const std::vector<std::string> &);

int main(int argc, char *argv[])
{
std::vector<std::string> theArgv;
for (int i = 0; i < argc; i++)
{
// a) or b) ?
}
return mymain(theArgv);
}

a) theArgv.push_back(argv[i]);

b) theArgv.push_back(std::string(argv[i]));

Any help is appreciated. :)

Kai-Uwe Bux 06-27-2008 01:31 AM

Re: A vector of strings
 
amphetaman@gmail.com wrote:

> Hello. I am writing code to put the traditional C-style command-line
> arguments into a std::vector of std::strings. I was wondering: which
> is the best way to do it? Are they identical?
>
> #include <string>
> #include <vector>
>
> int mymain(const std::vector<std::string> &);
>
> int main(int argc, char *argv[])
> {
> std::vector<std::string> theArgv;
> for (int i = 0; i < argc; i++)
> {
> // a) or b) ?
> }
> return mymain(theArgv);
> }
>
> a) theArgv.push_back(argv[i]);
>
> b) theArgv.push_back(std::string(argv[i]));
>
> Any help is appreciated. :)


What about

c)

int main ( int argn, char ** args ) {
std::vector< std::string > argument ( args, args+argn );



Best

Kai-Uwe Bux

amphetaman@gmail.com 06-27-2008 01:55 AM

Re: A vector of strings
 
> std::vector< std::string > argument ( args, args+argn );

Could you please explain that line in detail? I don't seem to
understand what it does.

Kai-Uwe Bux 06-27-2008 02:09 AM

Re: A vector of strings
 
amphetaman@gmail.com wrote:

>> std::vector< std::string > argument ( args, args+argn );

>
> Could you please explain that line in detail? I don't seem to
> understand what it does.


std::vector<T> has a constructor

template < Iterator >
vector ( Iterator from, Iterator to );

This constructor will construct a vector of Ts from any range whose elements
are convertible to T. Since char* converts to std::string, you can
construct a vector of strings from any range of char* Now, args is a
pointer to an array of char*, therefore

[args, args+argn)

is a range of char*.


Best

Kai-Uwe Bux



amphetaman@gmail.com 06-27-2008 11:32 AM

Re: A vector of strings
 
Oh, I get it now. Thanks! :)


Rolf Magnus 06-27-2008 11:38 AM

Re: A vector of strings
 
Kai-Uwe Bux wrote:

> What about
>
> c)
>
> int main ( int argn, char ** args ) {
> std::vector< std::string > argument ( args, args+argn );


Any particular reason for confusing people by calling the parameters of main
argn and args instead of the usual argc and argv?



All times are GMT. The time now is 08:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.