CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • ADO.NET Data Services in the .NET Framework
  • Visual C++ Programming: What's new for MFC library in VC++ 2010?
  • Microsoft Visual Studio LightSwitch and What It Can Do For You
  • Displaying Files and Folders in a GridView

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > CodeGuru Technical FAQs > CodeGuru Individual FAQs
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    CodeGuru Individual FAQs The indivdual FAQs for CodeGuru. See the specific Topic FAQ forums for index pages and links to these Frequently Asked/Answered Questions.

    Reply
     
    Thread Tools Search this Thread Display Modes
      #1    
    Old February 13th, 2003, 12:09 PM
    Yves M's Avatar
    Yves M Yves M is offline
    Moderator
    Power Poster
     
    Join Date: Aug 2002
    Location: Madrid
    Posts: 4,569
    Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)Yves M has much to be proud of (1500+)
    STL General: How to declare and use two-dimensional arrays ?

    Q: How can I define a dynamic two-dimensional array?

    A: The basic idea is to use a 'vector<vector<T> >'. Use containment to write a template class that offers the desired functionality. Following skeleton code shows how to implement and use such a class:

    Code:
    #include <vector>
    
    template <typename T>
    class dynamic_array
    {
    public:
      dynamic_array(){};
      dynamic_array(int rows, int cols)
      {
        for(int i=0; i<rows; ++i)
        {
          data_.push_back(std::vector<T>(cols));
        }
      }
      
      // other ctors ....
    
      inline std::vector<T> & operator[](int i) { return data_[i]; }
    
      inline const std::vector<T> & operator[] (int i) const { return data_[i]; }
    
      // other accessors, like at() ...
    
      void resize(int rows, int cols)
      {
        data_.resize(rows);
        for(int i = 0; i < rows; ++i)
          data_[i].resize(cols);
      }
    
      // other member functions, like reserve()....
    
    private:
      std::vector<std::vector<T> > data_;  
    };
    
    
    int main()
    {
      dynamic_array<int> a(3, 3);
      a[1][1] = 2;
      int x = a[1][1];
      return 0;
    }
    A different approach, not using the STL 'vector' class, is shown in the following FAQ...


    FAQ contributed by: [Gabriel Fleseriu] [Axter]



    Last edited by Andreas Masur; July 24th, 2005 at 12:55 PM.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > CodeGuru Technical FAQs > CodeGuru Individual FAQs


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes

    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 On
    Forum Jump


    All times are GMT -5. The time now is 11:04 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.