From: amphetaman on
I am writing code to put C-style command line arguments (int argc,
char *argv[]) into a vector of strings (std::vector<std::string>
theArgv). I have a question: Is it better to do

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

or

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

or are these identical?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thomas Maeder on
amphetaman(a)gmail.com writes:

> I am writing code to put C-style command line arguments (int argc,
> char *argv[]) into a vector of strings (std::vector<std::string>
> theArgv). I have a question: Is it better to do
>
> a) theArgv.push_back(argv[i]);
>
> or
>
> b) theArgv.push_back(std::string(argv[i]));
>
> or are these identical?

push_back() takes a T const & (i.e. std::string const &), so whether
or not your code explictily converts argv[i] to std::string, this
conversion is going to happen. Both versions are thus equivalent.


Is there anything wrong with

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

, though?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: peter koch larsen on
On 27 Jun., 22:10, ampheta...(a)gmail.com wrote:
> I am writing code to put C-style command line arguments (int argc,
> char *argv[]) into a vector of strings (std::vector<std::string>
> theArgv). I have a question: Is it better to do
>
> a) theArgv.push_back(argv[i]);
>
> or
>
> b) theArgv.push_back(std::string(argv[i]));

They are identical. Better than the loop would be to use std::copy
(from algorithm) with a back_inserter. Look it up and be wiser.

/Peter

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]