如何将std :: array C ++ 11操作转换为Boost + VS08? [英] How to translate std::array C++11 operations to Boost+VS08?

查看:83
本文介绍了如何将std :: array C ++ 11操作转换为Boost + VS08?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将这样的C ++ 11代码转换为Boost + visual studio 2008:在数组中进行多维数组创建和迭代,以防万一它属于某些集合?

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?

在这里:

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

神奇的是,改变了所有 std :: array boost :: array 是不够的=(

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<第4行矩阵= ... 部分给出了20种不同的相似的sintax错误...

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

所以我想知道什么是正确的翻译?

So I wonder what would be correct translation?

推荐答案

完成。该代码位于 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;

}

输出:

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

这篇关于如何将std :: array C ++ 11操作转换为Boost + VS08?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆