Advertisement

Operator []

Started by September 19, 2000 04:28 PM
10 comments, last by Crocmort 24 years, 3 months ago
Actually, the C++ STL vector implementations I've tried all work with multiple dimensions. Construction gets a bit nasty, but try running this:
std::vector< std::vector< int > > foo( 10, std::vector< int >( 10 ) );for( int j = 0; j < 10; j++ )    for( int k = 0; k < 10; k++ )        std::cout << foo[j][k] << std::endl; 

The important thing to note is that operator[] returns a reference to that element. It appears that the compiler is smart enough to figure the rest out. The other option is this: foo[j].operator[](k) but that's just scary

Edited by - Kensai on September 19, 2000 11:08:09 PM
Well there's no slick way of doing this. One way is to have a constructor that accepts arrays.

            CMatrix::CMatrix(int x, int y, int** array){	for(int x2=0;x2<x;x++)	{		for(int y2=0;y2<y;y++)		{			//init m_matrix dynamicaly or whatever			m_matrix[x2][y2] = array[x2][y2];			//lets just hope they send us the right size array			//a template class could force them to send the right size array			//but you may not want to complicate things.		}	}}array[2][2] = {{1,2},{2,1}};CMatrix matrix(2,2,array);            


Edited by - Atavist on September 19, 2000 11:06:50 PM
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.

This topic is closed to new replies.

Advertisement