2017-08-04 71 views
3

我有一對數組。我想在這個數組上使用memset來創建0,1或-1。我該怎麼做?我們總是這樣做是否正常嗎?memset C++中的一對數組

的對和數組是:

std::pair<int, int> ar[100][100]; 
+8

你幾乎從不會在C++中memset任何東西。只需使用初始化程序。 –

+0

你有一對數組對。所以你想要memset整個事情,或者只是一組數組? – juanchopanza

+4

你會如何將這個設置爲1「我們總是這樣做」? – melpomene

回答

4

不要在任何不是一個POD類型(std::pair不是)使用memset - 它不會順利(這是不確定的行爲做所以)。

帶構造函數的類需要那些被調用的構造函數 - memset不這樣做。非POD數據成員的類需要這些成員的構造函數 - memset不這樣做。

對於小陣列,您可以改爲使用aggregate initialization。 對於更大的陣列,您可以使用循環。 也有std::fill

如果在編譯時已知大小,我還建議在C風格的數組上使用std::array,否則建議使用std::vector

+1

'std :: pair'甚至不是一個類型。但是,知道在std :: pait 上使用memset有什麼問題會很有趣。 – juanchopanza

+0

@juanchopanza - 引用http://en.cppreference.com/w/cpp/string/byte/memset「如果對象不是普通可複製的(例如,標量,數組或C兼容結構),則行爲沒有定義。「和'std :: pair'不是可以複製的。 –

1

如前所述,一對不是POD。

但是該數據可以被重組爲具有效用的損失少一個POD:

#include <tuple> 
#include <utility> 
#include <cstring> 

/* this is a POD */ 
using pair_matrix = int [100][100][2]; 

extern std::tuple<int, int> foo(); 
extern void bar(std::tuple<int&, int&>); 

int main() 
{ 
    pair_matrix m; 

    /* we can memset PODS */ 
    std::memset(std::addressof(m), sizeof(m), 0); 

    /* we can convert the 2-d array into a tuple easily */ 
    auto at = [&m](int x, int y) { 
     auto& a = m[y][x]; 
     return std::tie(a[0], a[1]); 
    }; 

    /* and manipulate as a tuple */ 
    at(10,10) = foo(); 
    bar(at(10,10)); 
} 

另一個(可以說是更健壯的)的方法是去創造具有fill方法的多維數組類的概念:

#include <tuple> 
#include <utility> 
#include <memory> 

template<class Type, std::size_t...Dimensions> struct dimension_view; 
template<class Type> struct dimension_view<Type> 
{ 
    static constexpr std::size_t extent = 1; 
    static constexpr std::size_t size() { return extent; } 
    static constexpr std::size_t storage_size = 1; 

    using storage_type = Type; 

    constexpr dimension_view(Type* data) : data_(data) {} 

    operator Type&() { return *data_; } 

    template<class Other> 
    constexpr Type& operator=(Other&& other) { 
     *data_ = std::forward<Other>(other); 
     return *data_; 
    } 

    storage_type* data_; 
}; 

template<class Type, std::size_t Dim, std::size_t...Rest> 
struct dimension_view<Type, Dim, Rest...> 
{ 
    using next = dimension_view<Type, Rest...>; 
    static constexpr std::size_t extent = Dim; 
    static constexpr std::size_t size() { return extent; } 
    static constexpr std::size_t storage_size = Dim * next::storage_size; 

    using storage_type = Type [storage_size]; 

    constexpr dimension_view(Type* data) : data_(data) {} 

    auto begin() -> Type* 
    { 
     return data_; 
    } 
    auto end() -> Type* 
    { 
     return std::addressof(data_[extent * next::storage_size]); 
    } 

    auto operator[](std::size_t i) { 
     return next(std::addressof(data_[i * next::storage_size])); 
    } 

    Type* data_ ; 
}; 

template<class T, std::size_t...Dims> 
struct multi_dimensional_array 
{ 
    using view_type = dimension_view<T, Dims...>; 
    using storage_type = typename view_type::storage_type; 

    template<class...Args, std::enable_if_t<std::is_nothrow_constructible<T, Args...>::value>* = nullptr> 
    constexpr multi_dimensional_array(Args&&...args) 
    { 
     for(auto&& store : data_) 
     { 
      new (std::addressof(store)) T (args...); 
     } 
    } 

    template<class...Args, std::enable_if_t<not std::is_nothrow_constructible<T, Args...>::value>* = nullptr> 
    multi_dimensional_array(Args&&...args) 
    { 
     auto count = std::size_t(0); 
     try { 
      for(auto&& store : data_) 
      { 
       new (std::addressof(store)) T (args...); 
       ++count; 
      } 
     } 
     catch(...) { 
      destroy(count); 
     } 
    } 

    /* delete copies for now */ 
    multi_dimensional_array(multi_dimensional_array const&) = delete; 
    multi_dimensional_array& operator=(multi_dimensional_array const&) = delete; 
    multi_dimensional_array(multi_dimensional_array &&) = delete; 
    multi_dimensional_array& operator=(multi_dimensional_array &&) = delete; 

    ~multi_dimensional_array() 
    { 
     destroy(view_type::storage_size); 
    } 

    void destroy(std::size_t last) 
    { 
     while(last--) { 
      reinterpret_cast<T*>(get_data()+last)->~T();  } 
    } 

    constexpr auto begin_storage() { 
     return view_type(get_data()).begin(); 
    } 

    constexpr auto end_storage() { 
     return view_type(get_data()).end(); 
    } 

    constexpr auto fill(T value) -> void 
    { 
     std::fill(begin_storage(), end_storage(), value); 
    } 

    constexpr auto operator[](std::size_t i) -> decltype(auto) { 
     return view_type(get_data())[i]; 
    } 

    constexpr T* get_data() { 
     return reinterpret_cast<T*>(data_); 
    } 

    std::aligned_storage_t<sizeof(T), alignof(T)> data_[view_type::storage_size]; 
}; 

using pair_matrix = multi_dimensional_array<std::pair<int, int>, 100, 100>; 


void force_view(pair_matrix&); 
void force_deref(std::pair<int, int>&); 
std::size_t getval(); 

int main() 
{ 
    /* create with initial values */ 
    pair_matrix m(std::make_pair(-1, 1)); 

    /* fill the entire multidimentional array */ 
    m.fill(std::make_pair(-1, -1)); 

    m[10][10] = std::make_pair(1,1); 

    /* force compiler to generate some code to exercise implementation */ 
    force_view(m); 
    force_deref(m[getval()][getval()]); 
} 
1

C++的方法是使用std::fill代替memset,是這樣的:

std::pair<int, int> ar[100][100]; 
std::fill(&ar[0][0], &ar[0][0] + 100 * 100, std::make_pair(-1, -1));