[英]C++ std::vector - How to modify an element specified by an iterator?
[英]How to get the element in Vector using the specified position in c++?
如何通過在矢量模板中提供位置來獲取元素?
您可以像常規C數組一樣訪問std :: vector元素:
std::vector<int> myVector;
//(...)
int a = myVector[1];
你可以使用'at'函數(someVector.at(somePosition)獲取somePosition中的元素),或者你可以使用someVector [somePosition]。 這就像一個更發達的陣列。
使用at函數的區別在於,如果你給它一個無效的位置,它會拋出異常,而[] s不會檢查這樣的東西。
有兩種方法可以實現你想要的東西(對於矢量說Vec):
(1) Use at() function eg. Vec.at(index)
(2) Use like a normal array eg. Vec[index]
索引適用於矢量,因此只需使用索引即可。 類似於數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.