どうも、ちょげ(@chogetarou)です。
インデックスでVectorの最初の要素を取得する方法を紹介します。
方法

インデックスでVectorの先頭の要素を取得するには、[]を使います。
まず、Vectorの変数名と[]を記述します。
そして、[]内に「0」を指定します。
//myVec=対象のVector
T first = myVec[0];
上記の[]は、対象のVectorの先頭の要素を取得します。
使用例
#include <iostream>
#include <vector>
using namespace std;
int main(void){
vector<int> nums = { 1, 2, 3, 4, 5, 6 };
int first = nums[0];
cout << first<< endl;
return 0;
}
出力:
1
コメント