2D Vector Inside function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: 2D Vector Inside function

  1. #1
    Join Date
    Feb 2012
    Posts
    8

    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 ;
    
    } ;

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center