I know how to declare a 1D vector like below but how do you declare a 2D vector ?

List<String^>^ Vec1 = gcnew List<String^>();

Recommended Answers

All 19 Replies

Actually, that is a list, not a vector. Minor differences, but still.
Here's one way (matrix of strings):

typedef vector<string> row;

vector<row> *vec1 = new vector<row>;

Actually, that is a list, not a vector. Minor differences, but still.
Here's one way (matrix of strings):

typedef vector<string> row;

vector<row> *vec1 = new vector<row>;

That's not managed code, but managed code is probably something similar.

Thanks, a list it is but this declaration of a 2D vector in the previous post is this really for the System::IO namespace.

I know how to declare a 2D vector in the std:: namespace like this wich should be something simular to that:

typedef std::vector<string> String1D;
	typedef std::vector<String1D> String2D;

Is it possible to do the below to a 2D for the System::IO namespace. I am not really sure here.

List<String^>^ Vec1 = gcnew List<String^>();

Actually, that is a list, not a vector. Minor differences, but still.
Here's one way (matrix of strings):

typedef vector<string> row;

vector<row> *vec1 = new vector<row>;

It's exactly the same:

List<List<String^>^>^ vec = gcnew List<List<String^>^>();

Don't forget to use gcnew for each of the sublists too, and feel free to simplify the syntax using typedef as has already been shown.

I have come up with this code where I am creating a 2D list.
Then within a loop I think I am declaring 1000 elements to each dimension wich in the end
will have List[1000][1000].
However when I run the program, I will have an errormessage that says the below so there is something that I might doing wrong.
'Object reference not set to an instance of an object'

typedef List<String^>^ Vec1;
typedef List<Vec1> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	
	List1.Add(Vec1());	
         for( int i = 0; i < 1000; i++ )
	{
		List1[j]->Add("0");	
            }

}

List1[3][3] = "Hello";
typedef List<String^>^ Vec1;
typedef List<Vec1> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	
	Vec1 Row; 
	for( int i = 0; i < 1000; i++ )
	{
                Row.Add("0");
         }
         
          List1.Add(Row);
}

List1[3][3] = "Hello";

It should be something like this :)

Thanks Sci@Phy but if I run the program with the below code, I will still have the same Errormessage:) It seems that elements is Added but wonder what is being wrong.

'Object reference not set to an instance of an object'

typedef List<String^>^ Vec1;
typedef List<Vec1> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	
	Vec1 Row; 
	for( int i = 0; i < 1000; i++ )
	{
                Row->Add("0");
         }
         
          List1.Add(Row);
}

List1[3][3] = "Hello";

I have also tried this approach to declare elements in the 2 Dimensions but with the same Error.

typedef List<String^>^ Vec1;
typedef List<Vec1> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	

	List1.Add(Vec1()); //I suppose I add a First Dimension here ???

	for( int i = 0; i < 1000; i++ )
	{
		List1[j]->Add("0"); //I suppose I fill the First Dimension with 1000 elements
	}

}

I have tried this approach to declare elements in the 2 Dimensions but with the same Error.
'Object reference not set to an instance of an object'

typedef List<String^>^ Vec1;
typedef List<Vec1> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	

	List1.Add(Vec1()); //I suppose I add a First Dimension here ???
	for( int i = 0; i < 1000; i++ )
	{
		List1[j]->Add("0"); //Fill First Dimension 	}

}

Correct me if I'm wrong, but doesn't the cavet ^ symbol in C++.NET mean that the type is actually a pointer?

List1.Add(Vec1());

looks fishy... Vec1 is a pointer-type, so doesn't that mean you need to call gcnew to properly instantiate that Vec? O_O

List1.Add(gcnew Vec1());

I'd assume.

When using pointers in .NET, you do use * and & to address pointers so the cavet ^ symbol should only meen a type as int and double I beleive like string for the std:: and then String^ for managed.
If I put gcnew before Vec1 in the code below the compiler says this message:

'Vec1' : cannot use this type as argument of 'gcnew'

typedef List<String^>^ Vec1;
typedef List<Vec1> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	

	List1.Add(gcnew Vec1());

	for( int i = 0; i < 1000; i++ )
	{
		List1[j]->Add("0");
	}

}

I think you are after ...

typedef List<String^> Vec1;
typedef List<Vec1^> Vec2;

Vec2 List1;

for( int j = 0; j < 1000; j++ )
{	
	List1.Add(gcnew Vec1());

	for( int i = 0; i < 1000; i++ )
	{
		List1[j]->Add("0");
	}
}

Mitrmkar,

Thank you! Now it works...

When compiling the below code I will have a compileerror that says:
String3D : illegal use of this type as an expression
String2D : illegal use of this type as an expression
String1D : illegal use of this type as an expression

I wonder if I doing this correct when trying to iterate through all 3 Dimension with .Count ?

typedef List<String^> String1D;
typedef List<String1D^> String2D;
typedef List<String2D^> String3D;



	String3D Dimensions;	
	int count1 = 0;
	int count2 = -1;


for( int i = 0; i < 5; i++ )
{

	if( i == 0 )
	{
	          Dimensions.Add(gcnew String2D()); 
	}
	if( i >= 0 && i <= 4 )
	{
		count2 = count2 + 1;
		Dimensions[count1]->Add(gcnew String1D()); 
	}
	Dimensions[count1][count2]->Add("TextString");
} 


		
for(int h = 0; h < String3D.Count; ++h)
{
for(int j = 0; j < String2D.Count; ++j)
{
for(int k = 0; k < String1D.Count; ++k)
{
}}}

I found out that "()" was needed. When iterating through the dimensions to show the "TextString" that was put into the List(Dimensions), only 1 MessageBox will appear.
As I have put "TextString" to 5 elements it should be 5 Times.

I am putting the string to these elements:
Dimensions[0][0][0];
Dimensions[0][1][0];
Dimensions[0][2][0];
Dimensions[0][3][0];
Dimensions[0][4][0];

typedef List<String^> String1D;
typedef List<String1D^> String2D;
typedef List<String2D^> String3D;



	String3D Dimensions;	
	int count1 = 0;
	int count2 = -1;


for( int i = 0; i < 5; i++ )
{

	if( i == 0 )
	{
	          Dimensions.Add(gcnew String2D()); 
	}
	if( i >= 0 && i <= 4 )
	{
		count2 = count2 + 1;
		Dimensions[count1]->Add(gcnew String1D()); 
	}
	Dimensions[count1][count2]->Add("TextString");
} 


		
for(int h = 0; h < String3D().Count + 1; ++h)
{
for(int j = 0; j < String2D().Count + 1; ++j)
{
for(int k = 0; k < String1D().Count + 1; ++k)
{

MessageBox::Show(Dimensions[h][j][k]);

}


}}

What I try to find, is the equavilent to the vector ::iterator for the first code.
The first code iterates each Dimension from .begin() to the .end() and then continues
to next dimension for the 3 Dimensions.

typedef std::vector<string> String1D;
typedef std::vector<String1D> String2D;
typedef std::vector<String2D> String3D;

String3D Dimensions;	

for (String3D::iterator i = Dimensions.begin(); i != Dimensions.end(); ++i) 
{
for (String2D::iterator j = i->begin(); j != i->end(); ++j)			
{
for (String1D::iterator k = j->begin(); k != j->end(); ++k)			
{}}}

As all these Dimensions below for "List" have been ->Add and therefore have different many elements for each Dimension, I am looking for the same(equavilent) method to Iterate each Dimension to the end as the above approach for the std::vector.
Something might be missing in the below code.

typedef List<String^> String1D;
typedef List<String1D^> String2D;
typedef List<String2D^> String3D;

String3D Dimensions;	

for(int i = 0; i < String3D().Count + 1; ++i)
{
for(int j = 0; j < String2D().Count + 1; ++j)
{
for(int k = 0; k < String1D().Count + 1; ++k)
{}}}

In some way it is needed to refer to the previous vectordimension.
The problem is that I dont know how you refer to the previous dimension beginning and end and set this to some sort of an iterator as this is put ?

String2D::iterator j = i->begin(); j != i->end(); ++j

For the List, I cant find either a begin or end member or iterator.
The below code could be a step closer perheps.
Any idéas or tips would be of help. I am quite stuck.

for(int i = 0; i < Dimensions.Count; ++i)
{
for(int j = 0; j < String3D().Count; ++j)
{
for(int k = 0; k < String2D().Count; ++k)
{	
}}}

Yes, this was the thread I was reading on but I missed it and as I saw it was as simple as this. I was imagining me something different.
It helped alot. Great !

for(int i = 0; i < Dimensions.Count; ++i)
{
for(int j = 0; j < Dimensions[i]->Count; ++j)
{
for(int k = 0; k < Dimensions[i][j]->Count; ++k)
{
	
	MessageBox::Show(Dimensions[i][j][k]);

}


}
}

About this thread starting question: look at my posts in solved threads (two month ago):
http://www.daniweb.com/forums/thread136351.html
http://www.daniweb.com/forums/thread137814.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.