是否有可能獲得對數組元素的稀疏(非連續)子集?獲取數組的稀疏子集作爲參考
假設我有數組a = [1,4,5]
和索引pos = [0,1]
。我想獲得b = [1,5]
而不復制a
中的元素。換句話說,我想切入a
並創建一個名爲b
的視圖。
以下錯誤,因爲 「表達不可分配」:
#include <array>
int main() {
std::array<double,3> a = {1, 4, 5};
std::array<double, 2> b;
int c = 0;
int pos[2] = {0,2};
for (auto i = a.begin(); i != a.end(); i++) {
&b[c] = i;
i++;
}
return 0;
}
您不能擁有一個引用容器。你會得到最接近的是一個指針的容器,而不會複製'a'。 –
看看['std :: ref'](http://en.cppreference.com/w/cpp/utility/functional/ref) – StoryTeller