2015-03-03 68 views
3

我很驚訝地發現boost::multi_array似乎分配的初始元素不同於std::vector。它似乎沒有填充每個元素的唯一元素(使用其默認值或默認構造函數)。我無法找到關於此的更多信息。如何正確初始化一個boost multi_array對象?

有沒有辦法讓multi_array在每個元素上填充一個獨特的對象?

例如,請考慮以下幾點:

static int num = 0; 

struct A { 
    int n; 
    A() : n((::num)++) { 
     std::cout << "A()" << std::endl; 
    } 
    virtual ~A() {} 

    void print() { 
     std::cout << "n=" << n << std::endl; 
    } 
}; 

int main() { 
    std::cout << "vector:" << std::endl; 
    std::vector<A> v(3); 
    for (auto x : v) { 
     x.print(); 
    } 

    std::cout << "multi:" << std::endl; 
    boost::multi_array<A, 2> m(boost::extents[2][2]); 
    for (auto x : m) { 
     for (auto y : x) { 
     y.print(); 
     } 
    } 
} 

這將導致輸出:

vector: 
A() 
A() 
A() 
n=0 
n=1 
n=2 
multi: 
A() 
n=3 
n=3 
n=3 
n=3 

爲什麼只調用一次爲multi_array的構造? multi_array如何用獨特的對象「填充」(使用A的默認構造函數)?

+0

請注意'std :: vector'的行爲取決於您使用的C++版本!這是C++ 98和C++ 11標準之間的變化之一。 (在C++ 98中,第一個元素是構建的,其餘的都是複製的,在C++ 11中都是構建的)。 'multi_array'的行爲與舊的C++ 98'std :: vector'一致,並且在過去一致。可能需要更新(一致)版本的'multi_array'。 – alfC 2015-07-02 18:40:30

回答

3

快速填充整個數組這樣做fill_n¹:

std::fill_n(a.data(), a.num_elements(), 0); 

有了提升multi_array您可以使用以您自己的內存緩衝區,以獲得相同的性能(std::uninitialized_copy是你的朋友)。 (實際上,您甚至可以在現有內存上映射數組視圖,並且希望保留現有值)。

我寫了一個比較演示這個位置:pointers to a class in dynamically allocated boost multi_array, not compiling

Live On Coliru

#include <boost/multi_array.hpp> 
#include <type_traits> 
#include <memory> 

struct octreenode { int a; int b; }; 

class world { 
public: 
    world(double x, double y, double z, int widtheast, int widthnorth, int height) 
      : 
       originx(x), originy(y), originz(z), 
       chunkseast(widtheast), chunksnorth(widthnorth), chunksup(height) 
    { 
#define OPTION 4 

#if OPTION == 1 
     static_assert(std::is_trivially_destructible<octreenode>::value, "assumption made"); 
     //std::uninitialized_fill_n(chunk.data(), chunk.num_elements(), octreenode {1, 72}); 
     std::fill_n(chunk.data(), chunk.num_elements(), octreenode {1, 72}); 
#elif OPTION == 2 
     for(auto a:chunk) for(auto b:a) for(auto&c:b) c = octreenode{1, 72}; 
#elif OPTION == 3 
     for (index cz = 0; cz < chunksnorth; ++cz) { 
      for (index cx = 0; cx < chunkseast; ++cx) { 
       for (index cy = 0; cy < chunksup; ++cy) { 
        chunk[cz][cx][cy] = octreenode{1, 72}; 
       } 
      } 
     } 
#elif OPTION == 4 
     static_assert(std::is_trivially_destructible<octreenode>::value, "assumption made"); 
     for (index cz = 0; cz < chunksnorth; ++cz) { 
      for (index cx = 0; cx < chunkseast; ++cx) { 
       for (index cy = 0; cy < chunksup; ++cy) { 
        new (&chunk[cz][cx][cy]) octreenode{1, 72}; 
       } 
      } 
     } 
#endif 
     (void) originx, (void) originy, (void) originz, (void) chunksup, (void) chunkseast, (void) chunksnorth; 
    } 

private: 
    double originx, originy, originz; 
    int chunkseast, chunksnorth, chunksup; 

#if 1 
    typedef boost::multi_array<octreenode, 3> planetchunkarray; // a boost_multi for chunks 
    typedef planetchunkarray::index index; 
    planetchunkarray chunk{boost::extents[chunksnorth][chunkseast][chunksup]}; 
#else 
    static_assert(boost::is_trivially_destructible<octreenode>::value, "assumption made"); 

    std::unique_ptr<octreenode[]> raw { new octreenode[chunksnorth*chunkseast*chunksup] }; 
    typedef boost::multi_array_ref<octreenode, 3> planetchunkarray; 
    typedef planetchunkarray::index index; 
    planetchunkarray chunk{raw.get(), boost::extents[chunksnorth][chunkseast][chunksup]}; 
#endif 
}; 

int main() { 
    world w(1,2,3,4,5,6); 
} 

使用multi_array_ref的變體是如何避免拷貝構造元素的例子(這是類似於std::vector使用未初始化的內存用於保留但未使用的元素的優化)。


¹課程的唯一值,使用std::iotastd::generate

+0

我認爲'std :: array'可能是真的(對於純C/C++數組)。但'boost :: multi_array'確實會初始化它的元素。它的做法與'std :: vector'不同,正如我在發佈這個問題後發現的那樣。 – Corey 2015-03-04 01:58:39

+0

@Corey我已經用一些更多的信息擴展了我的答案,包括一個演示,演示如何使用multi_array_ref來實現就地構建元素。請記住:過早優化是所有邪惡的根源:) – sehe 2015-03-04 10:50:42

+0

感謝如何使用預分配內存的例子;這可能有用。在這種情況下,這實際上不是我的目標 - 我大多隻是困惑,爲什麼我沒有看到被調用的默認構造函數,並認爲我做錯了什麼。 ......我正在考慮接受我自己的答案,因爲它展示了開場白問題中的情況,這對未來有人發現這個問題可能有用。 – Corey 2015-03-05 04:19:11

1

所以在進一步的研究,我學會了兩兩件事:

  1. boost::multi_array使用複製構造函數將對象初始化到容器中,而不是默認的構造函數。

  2. 循環在C++ 11的for (auto x : container)方式似乎(至少鐺++ 3.5)遍歷拷貝容器元件的,而不是迭代器(或引用)。

修改原來的問題的例子來說明點1

將一個拷貝構造函數(和相應的計數器),並且使用auto& x爲對象循環,而不是auto x

static int num = 0; 
static int cpy = 0; 
struct A { 
    int n; 
    int c; 
    A() : n((::num)++), c(0) { 
     std::cout << "A_def()" << std::endl; 
    } 
    A(const A& o) : n(0), c((::cpy)++) { 
     std::cout << "A_cpy()" << std::endl; 
    } 
    virtual ~A() {} 

    void print() { 
     std::cout << "n=" << n << ",c=" << c << std::endl; 
    } 
}; 

int main() { 
    std::cout << "vector:" << std::endl; 
    std::vector<A> v(3); 
    for (auto& x : v) { 
     x.print(); 
    } 

    std::cout << "multi:" << std::endl; 
    boost::multi_array<A, 2> m(boost::extents[2][2]); 

    for (auto x : m) { 
     for (auto& y : x) { 
      y.print(); 
     } 
    } 
} 

產生輸出

vector: 
A_def() // <- vector allocation starts 
A_def() 
A_def() 
n=0,c=0 // <- vector printing starts, using "for (auto& x)" 
n=1,c=0 
n=2,c=0 
multi: 
A_def() // <- a temporary object for multi_array allocation 
A_cpy() // <- multi_array allocation starts 
A_cpy() 
A_cpy() 
A_cpy() 
n=0,c=0 // <- multi_array prints starts, using "for (auto& y)" 
n=0,c=1 
n=0,c=2 
n=0,c=3 

修改上面的例子如上面展示點2

同一個類定義這個答案,但去除對象環路auto& x,並要回使用auto x在原來的問題做。

std::cout << "vector:" << std::endl; 
    std::vector<A> v(3); 
    for (auto x : v) { 
     x.print(); 
    } 

    std::cout << "multi:" << std::endl; 
    boost::multi_array<A, 2> m(boost::extents[2][2]); 

    for (auto x : m) { 
     for (auto y : x) { 
      y.print(); 
     } 
    } 

生成輸出,顯示拷貝構造函數時print循環被調用,甚至在vector元素。

vector: 
A_def() // <- vector allocation starts 
A_def() 
A_def() 
A_cpy() // <- vector printing starts, using "for (auto x)" 
n=0,c=0 
A_cpy() 
n=0,c=1 
A_cpy() 
n=0,c=2 
multi: 
A_def() // <- a temporary object for multi_array allocation 
A_cpy() // <- multi_array allocation starts 
A_cpy() 
A_cpy() 
A_cpy() 
A_cpy() // <- multi_array printing starts, using "for (auto y)" 
n=0,c=7 
A_cpy() 
n=0,c=8 
A_cpy() 
n=0,c=9 
A_cpy() 
n=0,c=10 
+2

Derp。當然'for(auto x:c)'循環複製。也許你可以嘗試'for(auto&x:c)'循環左值(編輯:你注意到了啊,然後我不確定你爲什麼仍然指出它是多列數組有趣的) – sehe 2015-03-04 09:46:36

+0

@sehe I並不意味着將它指向'multi_array',這只是對原始問題需要做出的改變之一。關於'multi_array',重要的一點是它使用拷貝構造函數,而不是'vector'這樣的缺省值。 – Corey 2015-03-05 04:11:29