A vector of strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amphetaman@gmail.com

    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 ::stringtheArgv ;
    for (int i = 0; i < argc; i++)
    {
    // a) or b) ?
    }
    return mymain(theArgv) ;
    }

    a) theArgv.push_ba ck(argv[i]);

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

    Any help is appreciated. :)
  • Kai-Uwe Bux

    #2
    Re: A vector of strings

    amphetaman@gmai l.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 ::stringtheArgv ;
    for (int i = 0; i < argc; i++)
    {
    // a) or b) ?
    }
    return mymain(theArgv) ;
    }
    >
    a) theArgv.push_ba ck(argv[i]);
    >
    b) theArgv.push_ba ck(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

    Comment

    • amphetaman@gmail.com

      #3
      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.

      Comment

      • Kai-Uwe Bux

        #4
        Re: A vector of strings

        amphetaman@gmai l.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<Tha s 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


        Comment

        Working...