std::string array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SKSNET
    New Member
    • Feb 2008
    • 2

    std::string array

    Hi there,
    I am wondering that can a class member be an array of string. I can create an 2d array of char but I really want to use an array of std::string. Can any body help me out?

    NB: This is my first time posting any thread in any forum. I am not sure that I could explain my question. Please ask me if you need further details.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You can create a 2D array of strings, sure. If you have one of chars already, just replace char with string in the declaration. There is an issue with dynamic allocation and default constructors (you'd better supply one if you don't use {entry, entry} notation) since the arguments can't be passed to the constructor. Also, before weaknessforcats gets in here, read this howto on arrays in C/C++.

    [CODE=cpp]
    string mat[3][4];
    [/CODE]

    Comment

    • SKSNET
      New Member
      • Feb 2008
      • 2

      #3
      Thanx a lot for your help.

      Originally posted by Laharl
      You can create a 2D array of strings, sure. If you have one of chars already, just replace char with string in the declaration. There is an issue with dynamic allocation and default constructors (you'd better supply one if you don't use {entry, entry} notation) since the arguments can't be passed to the constructor. Also, before weaknessforcats gets in here, read this howto on arrays in C/C++.

      [CODE=cpp]
      string mat[3][4];
      [/CODE]

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You create an array of strings by using a vector.

        [code=cpp]
        vector<string> arr;
        [/code]

        A vector is implemented as a array. The vector member functions are the same functions you would need to write to use your C++ array. Why do the work over again?

        Comment

        Working...