Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:39:07+00:00 2026-06-12T11:39:07+00:00

I have the following code which seems to work: class MapCell { public: int

  • 0

I have the following code which seems to work:

class MapCell
{
    public:
    int x, y, z;
};

void Test3DVector(int size_x, int size_y, int size_z)
{
    vector< vector< vector<MapCell> > > m(size_x, vector<vector<MapCell>>(size_y, vector<MapCell>(size_z)));

    for (int x = 0; x < size_x; ++x)
    {
        for (int y = 0; y < size_y; ++y)
        {
            for (int z = 0; z < size_z; ++z)
            {
                m[x][y][z].x = x;
                m[x][y][z].y = y;
                m[x][y][z].z = z;
            }
        }
    }
}

I want to write a class that has the 3D vector as a member variable, with the dimensions set in the constructor, like so:

class MyClass
{
    public:
        vector< vector< vector<MapCell>>> m;    // I'm not sure how to write this
    MyClass::MyClass(int size_x, int size_y, int size_z)
    {
        // Do the same initialization as Test3DVector did.

    }
 };

So that I can do something like this:

void SomeFunction()
{
   MyClass grid(5, 5, 5);
   cout << grid->m[1][3][2].x << grid->m[1][3][2].y << grid->m[1][3][2].z << endl;
   // Should output 132
}

What is the best way to do this? When the vectors become very large are there any common mistakes that I should avoid to get the best speed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:39:09+00:00Added an answer on June 12, 2026 at 11:39 am

    updated with a variadic version

    1. Simple, explicit code

    class MyClass {
    public:
        vector<vector<vector<MapCell>>> m;
        MyClass(int size_x, int size_y, int size_z)
            : m(size_x,
                vector<vector<MapCell> >(
                    size_y,
                    vector<MapCell>(size_z)
                )
               ) {
        }
    };
    

    2. With a helper

    class MyClass {
        vector<vector<vector<MapCell>>> m;
    public:
        MyClass(int size_x, int size_y, int size_z)
            : m(Dim(size_z, Dim(size_y, Dim(size_x, MapCell())))) {
        }
    
    private:
        template <typename T>
        static std::vector<T> Dim(size_t n, T&& v) {
            return std::vector<T>(n, std::move(v));
        }
    };
    

    3. With a variadic helper

    Because variadics are funadic! here is a version using variadics to make things … prettier (depending on taste):

    struct MyClass
    {
        vector<vector<vector<MapCell>>> m;
        MyClass(int size_x, int size_y, int size_z)
            : m(vec_matrix(MapCell(), size_z, size_y, size_x))
        { }
    
    private:
        template <typename T> static std::vector<T> vec_matrix(T v, size_t n) {
            return { n, std::move(v) };
        }
    
        template <typename T, typename... Dim> static auto vec_matrix(T v, size_t n, Dim... other)
            -> std::vector<decltype(vec_matrix(v, other...))> {
            return { n, vec_matrix(v, other...) };
        }
    };
    

    PS. I have slightly edited the code to be more standards-compliant. There is a subtle issue with templates and trailing-return-type that refers to an overload of the same name.
    For a nice breakdown of the issue here, see this discussion bookmark in chat

    In case your compiler doesn’t believe it should work, see it live on gcc 4.7.2. It uses boost only to format the output:

    . . . .
    . . . .
    . . . .
    . . . .
    . . . .
    -------
    . . . .
    . . . .
    . . . .
    . . . .
    . . . .
    

    Full code to avoid link-rot on SO:

    #include <iostream>
    #include <vector>
    #include <boost/spirit/include/karma.hpp> // for debug output only
    
    using namespace std;
    
    struct MapCell 
    {
        friend std::ostream& operator <<(std::ostream& os, MapCell const&)
        { return os << "."; }
    };
    
    struct MyClass
    {
        vector<vector<vector<MapCell>>> m;
        MyClass(int size_x, int size_y, int size_z)
            : m(vec_matrix(MapCell(), size_z, size_y, size_x))
        { }
    
    private:
       ///////////////////////////////////////////////////
       // variadics are funadic!
       template <typename T> static std::vector<T> vec_matrix(T v, size_t n) 
       {
          return { n, std::move(v) };
       }
    
       template <typename T, typename... Dim> static auto vec_matrix(T v, size_t n, Dim... other)
          -> std::vector<decltype(vec_matrix(v, other...))> 
       {
          return { n, vec_matrix(v, other...) };
       }
       ////////////
    };
    
    int main()
    {
        MyClass a(4,5,2);
    
        using namespace boost::spirit::karma;
        std::cout 
            << format(stream % ' ' % eol % "\n-------\n", a.m) 
            << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so i have the following code setup which seems to work fine: user
Hello I have the following bit of code which seems to work, but I'm
I have implemented the IEquatable interface in a class with the following code. public
I have the following sample code which doesn't seem to want to run. import
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.