Thread zweidimensionale dynamische arrays: theorie + verständniss (12 answers)
Opened by Cooly at 2003-11-21 19:21

esskar
 2003-11-21 19:38
#11575 #11575
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
template <typename T>
class Array
{
  public:
     Array() : m_container(0), m_size(0), m_lastindex(0) { }
     Array(size_t size) : m_container(0), m_size(size), m_lastindex(0) { }
     ~Array() { if(m_container) free(m_container); }

     size_t CountElements() const { return m_lastindex; }
     size_t GetSize() const { return m_size; }

     bool ReSize(size_t size)
     {
        if(size <= m_size) return false;
       
        size_t len = sizeof(T) * size;
       
        if(m_container == 0)
        {
        m_container = (T*)malloc(len);
        memset(m_container, 0, len);
        }
        else
        {
           size_t oldlen = sizeof(T) * m_size;
        m_container = (T*)realloc(m_container, len);
        memset(m_container + oldlen, 0, len - oldlen);
        }        
        m_size = size;
        return true;
     }
     
     T & operator [](size_t idx)
     {
        if(idx >= m_size) ReSize(idx+1);
        return m_container[m_lastindex = idx];
     }

  private:
     T* m_container;
     size_t m_size;
     size_t m_lastindex;
};

Array< int > 1dintarray0(10);
Array< int > 1dintarray1(10);
Array< int > 1dintarray2(10);
Array< int > 1dintarray3(10);
Array< int > 1dintarray4(10);

Array< Array<int> > 2dintarray(5);

2dintarray[0] = 1dintarray0;
2dintarray[1] = 1dintarray1;
2dintarray[2] = 1dintarray2;
2dintarray[3] = 1dintarray3;
2dintarray[4] = 1dintarray4;


ungetestet ... sind bestimmt noch denkfehler drin...

empfehler dir std::vector aus der STL\n\n

<!--EDIT|esskar|1069634756-->

View full thread zweidimensionale dynamische arrays: theorie + verständniss