A vector of strings

Discussion in 'C++' started by amphetaman@gmail.com, Jun 27, 2008.

  1. Guest

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

    b) theArgv.push_back(std::string(argv));

    Any help is appreciated. :)
    , Jun 27, 2008
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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);
    >
    > b) theArgv.push_back(std::string(argv));
    >
    > 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
    Kai-Uwe Bux, Jun 27, 2008
    #2
    1. Advertising

  3. Guest

    > 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.
    , Jun 27, 2008
    #3
  4. Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Jun 27, 2008
    #4
  5. Guest

    Oh, I get it now. Thanks! :)
    , Jun 27, 2008
    #5
  6. Rolf Magnus Guest

    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?
    Rolf Magnus, Jun 27, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,725
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    733
    Malcolm
    Jun 24, 2006
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    346
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page