string arrays?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Soneji
    New Member
    • Apr 2007
    • 36

    string arrays?

    I was thinking about this, and I decided to ask...

    Is it acceptable in the C++ world to make an array out of a string object?

    Example:
    [Code=cpp]
    std::string str[50];
    [/Code]

    Now, before the answers start flowing, let me state: I am NOT planning on doing this.

    I recently learned how to use vectors, so I would use a string vector before I would do the above.

    I know it's possible to do ( I've done it ), but I've been wondering if it was "ok" do this.

    It's not that important, I just thought, maybe someone out there uses this in their programs.


    Thanks for your time!

    -Soneji
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Soneji
    I was thinking about this, and I decided to ask...

    Is it acceptable in the C++ world to make an array out of a string object?

    Example:
    [Code=cpp]
    std::string str[50];
    [/Code]

    Now, before the answers start flowing, let me state: I am NOT planning on doing this.

    I recently learned how to use vectors, so I would use a string vector before I would do the above.

    I know it's possible to do ( I've done it ), but I've been wondering if it was "ok" do this.

    It's not that important, I just thought, maybe someone out there uses this in their programs.


    Thanks for your time!

    -Soneji
    I think it's Ok. I use it sometimes. It's the same thing as "char * str[50];". But I'm sure someone else can give a more technical reason.

    Comment

    • ljfong
      New Member
      • Jun 2007
      • 1

      #3
      Originally posted by Soneji
      I was thinking about this, and I decided to ask...

      Is it acceptable in the C++ world to make an array out of a string object?

      Example:
      [Code=cpp]
      std::string str[50];
      [/Code]

      Now, before the answers start flowing, let me state: I am NOT planning on doing this.

      I recently learned how to use vectors, so I would use a string vector before I would do the above.

      I know it's possible to do ( I've done it ), but I've been wondering if it was "ok" do this.

      It's not that important, I just thought, maybe someone out there uses this in their programs.


      Thanks for your time!

      -Soneji
      It's ok if for some reason you have to dispense with the overheads that come with vectors. Vectors are basically "enhanced" version of arrays, with better type safety, better memory management, better bound checking, and so on. When memory and/or space is at absolute premium, such as coding for embedded devices, I would imagine you would dispense even with std::string, preferring to do with straight char*, or char** for array of strings.

      Comment

      • plemoine
        New Member
        • Jun 2007
        • 15

        #4
        std::vector is higly recommended (see Scott Meyer books - Effective C++ / Effective STL).
        [Code=cpp]
        using namespace std;

        vector<string> strVec;
        strVec.reserve( 50);
        strVec.push_bac k(". . .");
        strVec.push_bac k(". . .");
        [/Code]

        Originally posted by Soneji
        I was thinking about this, and I decided to ask...

        Is it acceptable in the C++ world to make an array out of a string object?

        Example:
        [Code=cpp]
        std::string str[50];
        [/Code]

        Now, before the answers start flowing, let me state: I am NOT planning on doing this.

        I recently learned how to use vectors, so I would use a string vector before I would do the above.

        I know it's possible to do ( I've done it ), but I've been wondering if it was "ok" do this.

        It's not that important, I just thought, maybe someone out there uses this in their programs.


        Thanks for your time!

        -Soneji

        Comment

        • Soneji
          New Member
          • Apr 2007
          • 36

          #5
          Thanks for the answers guys ( and/or girls ) :)

          I appreciate you taking the time to respond.

          I'm out, Lates!

          -Soneji

          Comment

          Working...