-
February 8th, 2012, 05:05 PM
#1
2D Vector Inside function
Hi CodeGuru ,
In here, i saw that a 2D vector can be created in the following way. It really works .
What if we want to create and initialize a 2D vector inside a function, what should be the syntax ??
Thanks in advance.
Code:
# include <cliext/vector>
using namespace cliext;
using namespace System;
ref class MyClass {
public:
MyClass()
{
vector2D = gcnew vector<vector<int>^>(13);
for(int i=0;i<13;i++)
vector2D[i] = gcnew vector<int>(13);
}
vector<vector<int>^> ^vector2D ;
} ;
-
February 8th, 2012, 08:33 PM
#2
Re: 2D Vector Inside function
Do you intend to initialize a class member 2D vector from an arbitrary member function or do you want a 2D vector as a function-local variable? For the former you can just use the proposed syntax for initializing it from the constructor. In this respect the constructor and any other (non-static) member function are equivalent (but note that there are some fundamental aspects where they are not equivalent). For the latter (which I consider to be more likely), unlike the initialization of non-static member variables, you can use the "assign while declaring" initialization syntax; creation of the inner vectors would then be as we already had it:
Code:
MyMemberFunction()
{
vector<vector<int> ^> ^vector2D = gcnew vector<vector<int> ^>(13);
for (int i = 0; i < vector2D->size(); ++i)
vector2D[i] = gcnew vector<int>(13);
// Do whatever you want...
}
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|