Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A vector of strings

Reply
Thread Tools

A vector of strings

 
 
amphetaman@gmail.com
Guest
Posts: n/a
 
      06-27-2008
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.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
amphetaman@gmail.com
Guest
Posts: n/a
 
      06-27-2008
> 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.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-27-2008
(E-Mail Removed) 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


 
Reply With Quote
 
amphetaman@gmail.com
Guest
Posts: n/a
 
      06-27-2008
Oh, I get it now. Thanks!

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-27-2008
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments