如何翻译的std ::阵列C ++ 11倍的操作,以提高+ VS08?(How to transla

2019-09-28 04:15发布

我不知道有这样的C ++ 11码转换成升压+ Visual Studio 2008中:在一些情况下收集其部分multydimensional阵列的创建和迭代通呢?

这里有云:

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

它magicallyappeared,改变所有std::arrayboost::array是不够=(

#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 = ...部分给出像20个不同的一样sintax错误...

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

Answer 1:

完成。 该代码已出现在http://ideone.com/ATY4q

也固定在递归范围检查一个明显的错误。 你能发现它?

#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


Answer 2:

std::array是一个聚集 ,所以你应该能够底层阵列上直接使用集合初始化语法。 也就是说,不是这个,:

typedef std::array<int, 4> V4;
typedef std::array<V4, 4> M44;

M44 m { { 1,2,3,4}, {3,4,5,6}, {2,1,3,2}, {1,5,3,2} };

你可以只写一个赤裸裸的数组:

int[4][4] m = { { 1,2,3,4}, {3,4,5,6}, {2,1,3,2}, {1,5,3,2} };


Answer 3:

这将是有些难以效仿通用的初始化程序语法。 您可能需要单独分配要素,创造职位。

您还可以更换auto为明确的类型的关键字,并切换到旧的for循环语法。

事实上,这整个文件看起来像新的C ++ 11种功能的演示。 你可能会更好从头开始,并作出设计,更友好的C ++ 03的限制。



Answer 4:

我相信,你的问题是因为VS2008(和其他大多数前置C ++ 11编译)不支持“初始化列表”,这是你如何初始化在C ++ 11用户定义的集合。

例如,你不能说

向量<int>的V = {1,2,3,4,5};

在C ++ 03,但你可以在C ++ 11。

而且,当然,你不能使用“自动”两种。

auto areas = getareas(matrix); 

还是新的for循环语法:

for (auto pt : area.cells) 


Answer 5:

的声明matrix是用C ++ 11的初始化语法; 想必,你的编译器不支持。 有没有简单的方法来初始化多维boost::array ; 你需要每个元素之后分配。

boost::array<row, 4> matrix;
matrix[0][0] = 1;
// ...
matrix[3][2] = 1;

或者,你可以写一个make_array是组装它的参数到一个数组功能。 然而,如果没有可变参数模板,你需要写一个重载要支持每个数组的大小。

boost::array<row, 4> matrix = make_array(
    make_array(1,2,3),
    make_array(1,3,3),
    make_array(1,3,3),
    make_array(100,2,1));

另一种可能性是使用一个普通的阵列,和过载getareas为:

template <typename T, size_t Rows, size_t Cols>
std::vector<area<T> > getareas(T (&matrix)[Rows][Cols]);

int matrix[4][3] = {{1,2,3}, {1,3,3}, {1,3,3}, {100,2,1}};

auto areas = getareas(matrix); // if "auto" works for you


文章来源: How to translate std::array C++11 operations to Boost+VS08?