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 6814931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:49+00:00 2026-05-26T20:44:49+00:00

I wonder how to translate such C++11 code into Boost+visual studio 2008: multydimensional array

  • 0

I wonder how to translate such C++11 code into Boost+visual studio 2008: multydimensional array creation and iteration thru it in case its part of some collection?

Here it goes:

#include <iostream>
#include <array>
#include <vector>
#include <set>

typedef size_t cell_id; // row * COLS + col

template <typename T> struct area
{
    T value;
    std::vector<cell_id> cells;
};

template <typename T, size_t Rows, size_t Cols>
std::vector<area<T> > getareas(const std::array<std::array<T, Cols>, Rows>& matrix)
{
    std::vector<area<T> > areas;
    return areas;
}


int main(){
    typedef std::array<int, 3> row;
    std::array<row, 4> matrix = { 
        row { 1  , 2, 3, },
        row { 1  , 3, 3, },
        row { 1  , 3, 3, },
        row { 100, 2, 1, },
    };

    auto areas = getareas(matrix);

    std::cout << "areas detected: " << areas.size() << std::endl;
    for (const auto& area : areas)
    {
        std::cout << "area of " << area.value << ": ";
        for (auto pt : area.cells)
        {
            int row = pt / 3, col = pt % 3;
            std::cout << "(" << row << "," << col << "), ";
        }
        std::cout << std::endl;
    }
}

it magicallyappeared that changing all std::array to boost::array is not enough =(

#include <iostream>
#include <array>
#include <vector>
#include <set>
#include <boost/array.hpp>

typedef size_t cell_id; // row * COLS + col

template <typename T> struct area
{
    T value;
    std::vector<cell_id> cells;
};

template <typename T, size_t Rows, size_t Cols>
std::vector<area<T> > getareas(const boost::array<boost::array<T, Cols>, Rows>& matrix)
{
    std::vector<area<T> > areas;
    return areas;
}


int main(){
    typedef boost::array<int, 3> row;
    boost::array<row, 4> matrix = { 
        row { 1  , 2, 3, },
        row { 1  , 3, 3, },
        row { 1  , 3, 3, },
        row { 100, 2, 1, },
    };

    auto areas = getareas(matrix);

    std::cout << "areas detected: " << areas.size() << std::endl;
    for (const auto& area : areas)
    {
        std::cout << "area of " << area.value << ": ";
        for (auto pt : area.cells)
        {
            int row = pt / 3, col = pt % 3;
            std::cout << "(" << row << "," << col << "), ";
        }
        std::cout << std::endl;
    }
}

boost::array<row, 4> matrix = ... part gives like 20 different alike sintax errors…

So I wonder what would be correct translation?

  • 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-05-26T20:44:50+00:00Added an answer on May 26, 2026 at 8:44 pm

    Done. The code is live on http://ideone.com/ATY4q

    Also fixed a glaring bug in the recursion range checking. Can you spot it?

    #include <iostream>
    #include <fstream>
    #include <boost/assign.hpp>
    #include <boost/array.hpp>
    #include <vector>
    #include <set>
    
    namespace mxdetail
    {
        typedef size_t cell_id; // row * COLS + col
    
        template <typename T> struct area
        {
            T value;
            typedef std::vector<cell_id> cells_t;
            cells_t cells;
        };
    
        template <typename T, size_t Rows, size_t Cols>
            std::vector<area<T> > getareas(const boost::array<boost::array<T, Cols>, Rows>& matrix)
        {
            typedef boost::array<boost::array<T, Cols>, Rows> mtx;
            std::vector<area<T> > areas;
    
            struct visitor_t
            {
                const mtx& matrix;
                std::set<cell_id> visited;
    
                visitor_t(const mtx& mtx) : matrix(mtx) { }
    
                area<T> start(const int row, const int col)
                {
                    area<T> result;
                    visit(row, col, result);
                    return result;
                }
    
                void visit(const int row, const int col, area<T>& current)
                {
                    const cell_id id = row*Cols+col;
                    if (visited.end() != visited.find(id))
                        return;
    
                    bool matches = current.cells.empty() || (matrix[row][col] == current.value);
    
                    if (matches)
                    {
                        visited.insert(id);
                        current.value = matrix[row][col];
                        current.cells.push_back(id);
    
                        // process neighbours
                        for (int nrow=std::max(0, row-1); nrow < std::min((int) Rows, row+2); nrow++)
                        for (int ncol=std::max(0, col-1); ncol < std::min((int) Cols, col+2); ncol++)
                            /* if (ncol!=col || nrow!=row) */
                                visit(nrow, ncol, current);
                    }
                }
            } visitor(matrix);
    
            for (int r=0; r < (int) Rows; r++)
                for (int c=0; c < (int) Cols; c++)
                {
                    mxdetail::area<int> area = visitor.start(r,c);
                    if (!area.cells.empty()) // happens when startpoint already visited
                        areas.push_back(area);
                }
    
            return areas;
        }
    }
    
    
    template <typename T, size_t N>
       boost::array<T, N> make_array(const T (&a)[N])
    {
        boost::array<T, N> result;
        std::copy(a, a+N, result.begin());
        return result;
    }
    
    int main()
    {
        typedef boost::array<int, 3> row;
    
        int row0[] = { 1  , 2, 3, };
        int row1[] = { 1  , 3, 3, };
        int row2[] = { 1  , 3, 3, };
        int row3[] = { 100, 2, 1, };
    
        boost::array<row, 4> matrix;
        matrix[0] = make_array(row0);
        matrix[1] = make_array(row1);
        matrix[2] = make_array(row2);
        matrix[3] = make_array(row3);
    
        typedef std::vector<mxdetail::area<int> > areas_t;
        typedef areas_t::value_type::cells_t cells_t; 
    
        areas_t areas = mxdetail::getareas(matrix);
        for (areas_t::const_iterator it=areas.begin(); it!=areas.end(); ++it)
        {
            std::cout << "area of " << it->value << ": ";
            for (cells_t::const_iterator pit=it->cells.begin(); pit!=it->cells.end(); ++pit)
            {
                int row = *pit / 3, col = *pit % 3;
                std::cout << "(" << row << "," << col << "), ";
            }
            std::cout << std::endl;
        }
        std::cout << "areas detected: " << areas.size() << std::endl;
    
    }
    

    Output:

    area of 1: (0,0), (1,0), (2,0), 
    area of 2: (0,1), 
    area of 3: (0,2), (1,1), (1,2), (2,1), (2,2), 
    area of 100: (3,0), 
    area of 2: (3,1), 
    area of 1: (3,2), 
    areas detected: 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently working on Speech using microsoft translate. Just wonder if I can request by
i wonder if i can use google translate api to translate ajax embedded texts
I wonder how to translate this sql query to use the Criteria API. If
I wonder if someone can help me translate a MySQL query to a (Db)LINQ
In ASCII, i wonder how is 65 translated to 'A' character? As far as
Wonder what the difference between: static PROCESSWALK pProcess32First=(PROCESSWALK)GetProcAddress(hKernel,Process32First); ... pProcess32First(...); what is hKernel? Look
Wonder if anyone can help me understand how to sum up the column of
wonder why when I export my project to *.air and I install it on
wonder what's wrong <table id=tblDomainVersion> <tr> <td>Version</td> <td>No of sites</td> </tr> <tr> <td class=clsversion>1.25</td>
wonder whether someone can help me with the following one... I have a struct

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.