English 中文(简体)
如何翻译:C++11号行动,以波斯特+VS08?
原标题:How to translate std::array C++11 operations to Boost+VS08?

我想知道,如何将这种C++11的法典转化为2008年“Bost+”视频演播室:如果是某些收藏的一部分,那么多功能阵列的制作和振荡就是这样吗?

这里是:

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

它对修改所有<条码>的以下文字表示惊讶: 仅限:

#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>map = ...... part given as 20 different sintax mis...

因此,我想知道什么是正确的翻译?

最佳回答

Done。 该守则在

此外,在再保险范围检查中还固定了一条冰川。 你们能否知道?

#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 is an aggregate,因此,你应当能够直接使用基底阵列上的合计初始化辛奈。 这不是:

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

You can just write a naked array:

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

It s going to be somewhat difficult to emulate the generalized initializer syntax. You may have to assign elements individually, post-creation.

您还必须用明确类型取代<条码>auto的关键词,转而代之以代号。

In fact, this whole file looks like a demonstration of the new C++11 features. You ll probably be better off starting over, and making a design that s more friendly to the limitations of C++03.

我认为,你的问题是,因为VS2008(和大多数其他的C++11汇编者)不支持“初始编制者名单”,而这正是你在C++11中启动用户定义的收集工作。

例如,你不能说。

a. 病媒与底体;int> v ={1,2,3,4,5};

in C++03, but you can in C++11.

And, of course, you can t use "auto" either.

auto areas = getareas(matrix);

或新加入:

for (auto pt : area.cells)

<代码>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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签