vector::front, vector::back を使ってみる (C++)
C++
Published: 2020-07-08

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {5, 3, 2, 9, 8};

    cout << "v.front(): " << v.front() << endl;
    cout << "v.back(): " << v.back() << endl;

    // インデックス直接指定でもできる
    cout << "v[0]: " << v[0] << endl;
    cout << "v[v.size() - 1]: " << v[v.size() - 1] << endl;
}

出力結果

v.front(): 5
v.back(): 8
v[0]: 5
v[v.size() - 1]: 8

参考