home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,943 of 33,346   
   Wil Evers to Seungbeom Kim   
   Re: Passing multi-dimensional arrays to    
   26 Mar 13 19:28:39   
   
   From: bouncer@dev.null   
      
   Seungbeom Kim wrote:   
      
   > On 2013-03-25 13:43, Francis Glassborow wrote:   
   >>   
   >> And in C++, do not use arrays, use a vector of vectors instead.   
   >   
   > I'm not sure if a M-element vector of N-element vectors is better than   
   > a M*N-element vector. You'll have M+1 allocations, worse locality,   
   > and weird and much more verbose declarations:   
   >   
   >      std::vector> matrix(M, std::vector(N));   
   >      std::vector>::size_type sz = matrix.size();   
   >      std::vector>::const_iterator it = matrix.end();   
   >   
   > in exchange for the nice access syntax 'm[i][j]'.   
      
   [snip]   
      
   > What's the canonical way to have a multidimensional array in C++?   
   > What a pity I haven't found the answer yet. So I just allow myself to   
   > use the C arrays 'T a[M][N];', at least for simple cases.   
      
   Well, for this simple case (that is, a fixed-size two-dimensional   
   array of doubles), in C++11, I would use std::array, M>.   
      
   std::array is as fast as a built-in C-style array, uses no   
   heap allocation, has excellent locality of reference, and comes without   
   the irregularities of built-in C-style arrays.  I like it.   
      
   - Wil   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca