2017-01-02 89 views
0

的載體。我只有這個:如何添加元素的元組

std::vector<int[2]> ints; 

哪能元素加入到這個載體?

使用​​或ints.push_back()

不知道該怎麼做,C/C++ newb。

+2

我很確定矢量是一樣的載體 ... so push_back(&some_int)?如果你正在尋找對,你應該使用std :: vector > ints; – ChaoSXDemon

+0

'std :: vector int;'甚至不會編譯某些編譯器,你的目標是什麼? –

+1

'int [2]'無效,因爲數組不可複製或移動 – vu1p3n0x

回答

3

這裏有一種方法可以實現你正在尋找的功能:

std::vector<std::tuple<int, int>> ints; 

然後你就一個元組添加到載體是這樣的:

ints.push_back(std::make_tuple(1, 2)); 

編輯/更新: 如果你正在循環你的矢量和i是你在循環中使用的整數索引,然後訪問元組,你可以這樣做:

int intOne, intTwo; 

intOne = std::get<0>(ints[i]); 
intTwo = std::get<1>(ints[i]); 

這裏是get for a tuple

+0

謝謝,你能演示如何訪問元組(我可以訪問一個元素的向量,但不知道如何訪問元組 – Olegzandr

+0

@Olegzandr訪問元組的信息已添加 – JustSomeDude

+0

這是多麼bueno grassias – Olegzandr

3

用C++ 11:

std::vector<std::tuple<int, int, int>> vec; 
vec.emplace_back(0, 1, 2);