你可以定義你的custom_array,它是從標準std :: array繼承而來的。
template<typename _T, size_t _size>
class custom_array : public std::array<_T, _size> {
public:
custom_array(std::array<_T, _size> arr,
size_t start,
size_t end) {
size_t itr = 0;
while (itr < this->size() &&
start < arr.size() &&
start <= end) {
this->_Elems[itr++] = arr[start++];
}
}
custom_array(std::array<_T, _size>::iterator start,
std::array<_T, _size>::iterator end) {
size_t itr = 0;
while (itr < this->size()) {
this->_Elems[itr++] = *start;
start++;
if (start == end) { break; }
}
}
};
然後你就可以宣佈你custom_array如你所願,例如:
std::array<int, 4> arr = { 1, 2, 3, 4 };
custom_array<int, 4> arr_1(a, 0, 4);
custom_array<int, 4> arr_2(a.begin(), a.end());
看看一些基本參考材料,就會發現,在構造函數是隱式聲明,並且必須遵循集合初始化規則。由於這不包括迭代器,答案是否定的。請記住,'std :: array'代表一個固定/靜態數組,就像傳統的C風格數組一樣。 –
你打算使用C++ 17嗎? http://en.cppreference.com/w/cpp/container/array/begin – Rama
既然你知道數組的大小,你可以計算'* std :: begin(myarray)','* std :: next( std :: begin(myarray))'等,並用通常的聚合語法'std :: array myarray2 {a,b,c,d};構建myarray2。 –