2012-04-17 100 views
1

我是C++的新手,我花了一晚思考這個問題。我想創建一個2維數組,給出了第一維的長度。第二維的長度從1增加。對於二維數組a [] [],a [0] []有1個元素,a [1] []有2個元素,a [2] []有3個元素等。C++中的二維步長陣列

它聽起來不響像一個堅硬的結構,但我找不到一個創建它 - 我所能做的就是創建ax * x數組,這意味着一半的空間都浪費在我身上。

任何人有任何想法?提前致謝。

+4

除非它是一個非常大的數組,那麼最簡單的方法是隻不擔心「浪費」的空間。 – 2012-04-17 21:56:51

+0

如果你可以使用矢量,他們是完美的。 – chris 2012-04-17 21:56:51

回答

1

std::vector解決方案:

vector< vector<int> > stairs; 

for(int i = 0; i < n; i++) // n is size of your array 
    stairs[i].resize(i+1); 

你也可以做到這一點使用普通指針:

int * stairs[n]; 
for(int i = 0; i < n ; i++) 
    stairs[i] = new int[i+1]; 

但是這時候你就不用擔心刪除這樣的特徵時不再需要它。

+2

你應該不鼓勵新的C++用戶使用原始指針和不受保護的動態內存分配:) – enobayram 2012-04-17 22:19:30

+0

這似乎是一個easist解決方案。我沒有時間嘗試,但我會盡快做到,並回復你。此外,我會嘗試忽略「浪費的空間」,看看如何影響性能。 – 2012-04-19 09:53:49

+0

運行後,我認爲這是我想要的。謝謝。我也試過這個: array < vector>樓梯; 因爲我知道數組的大小,但似乎C++不允許這樣做。 – 2012-04-19 16:27:29

1

請嘗試考慮您的陣列的動態分配。

Dynamic array allocation

另一種方法,使多維數組使用已知 作爲指針的指針的一個概念。就像羅恩星期四所說的,大多數人認爲像行電子表格這樣的二維數組(這只是 很好)的二維數組,但是在'引擎蓋下',C++使用的是ptr到ptrs。首先,你首先創建一個基址指針。接下來,分配一行 指針並將第一個地址分配給基址指針。 接下來,分配的內存來容納每一行的列數據和行指針數組

但在分配 地址,如果你是新的CPP,我以爲你不會來處理大量的數據,所以不要擔心內存!

1

一個解決方案是定義一個類,該類包含大小爲x *(x + 1)/ 2的單維數據數組,並且過載爲type & operator()(int r, int c)以執行正確的索引類型。

template<class datatype, int size> 
class strange2dArray { 
    datatype data[size*(size+1)/2]; 

    datatype & operator()(int r, int c) { 
     // assert if the indexes are correct 
     return data[r*(r+1)/2+c]; 
    } 
}; 

BTW,除非你做這個學習C++,你應該使用某種數學庫(或其他)的爲您提供這樣的基本數據結構。他們會更高效和安全地實施它。

0

首先,讓我們看到的Python測試:

>>> a=[] 
>>> a[0]=3 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IndexError: list assignment index out of range 
>>> a={} 
>>> a[0]=3 

哎呀,看起來像陣列,簡化版,意味着它是數組。如果你想要動態大小的「數組」,你可以使用映射。 是的,這是第一個解決方案:

#include <map> 
#include <iostream> 
using namespace std; 

typedef std::map<int, int> array_d2; //length of second dimensional is increased 
array_d2 myArray[10] ; //length of first dimensional is given 


int main() 
{ 
myArray[0][1] = 3; 
myArray[0][2] = 311; 

//following are tests 
cout << myArray[0][1] << endl; 
cout << myArray[0][2] << endl; 

return 0; 
} 

(輸出爲:)

$ ./test 
3 
311 

我的第二個解決方案是使用的東西更像是一個數組,但有大小調整功能,你應該重寫opertation []使其自動爲用戶。

#include <vector> 
#include <iostream> 
using namespace std; 

//length of second dimensional is increased 
class array_d2 { 
    int m_size; 
    vector<int> m_vector; 
    public: 
    array_d2 (int size=10) { 
     m_size = size; 
     m_vector.resize(m_size); 
    }; 
    int& operator[] (int index) { 
     if (index >= m_size) { 
     m_size = index + 1; 
     m_vector.resize(m_size); 
    } 
    return m_vector[index]; 
    }; 
}; 

array_d2 myArray[10] ; //length of first dimensional is given 


int main() 
{ 
myArray[0][1] = 3; 
myArray[0][20] = 311; 
myArray[1][11] = 4; 
myArray[1][12] = 411; 


//following are tests 
cout << myArray[0][1] << endl; 
cout << myArray[0][20] << endl; 
cout << myArray[1][11] << endl; 
cout << myArray[1][12] << endl; 

return 0; 
} 

(輸出)

$ ./test1 
3 
311 
4 
411