CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #113

at() Throws, [] Does Not Check

easyExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3#include <vector>
4 
5int main() {
6 std::vector<int> scores = {70, 85, 90};
7 int positions[] = {0, 2, 3, 1};
8 for (int pos : positions) {
9 try {
10 int value = scores.at(pos);
11 std::cout << "position " << pos << ": " << value << "\n";
12 } catch (const std::out_of_range&) {
13 std::cout << "position " << pos << " is outside the vector\n";
14 }
15 }
16 std::cout << "size " << scores.size() << "\n";
17 return 0;
18}

🎯 Your Answer