0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vector では v[i] より v.at(i)と書こう

0
Posted at

当たり前なのかもしれないが、知らなかったので備忘録として。
初投稿だが、こういう時のためにQuitaを使うのかというお気持ち。

さっそく本題だが、競プロで合ってるはずなのになぜかWAになることが多く、その原因の多くはインデックス周りが原因だった。
自分の環境ではvectorの配列外参照はコンパイルが通ってしまうから余計にたちが悪い。

vectorを使ってランダムアクセスをする時は .at(i) を使うと配列外参照するとちゃんとエラーが出てきてくれて嬉しかった。

vector<int> v{0,1,2,3,4};
cout<<v[5]<<endl; // 0
cout<<v.at(5)<<endl; // Abort trap: 6

『江添亮のC++入門』より引用

さあ皆さんご一緒に、
・コンパイルエラーは普通
・コンパイルエラーが出たらありがとう
・コンパイルエラーが出たら大喜び

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?