sum of specific elements in a vector

Is it possible to add specific elements of a vector? I am not sure as to how to go about doing this.

below is an example of something i would like sum to achieve
1
2
3
4
5
6
7
8
9
#include<vector>

vector<int>arr;;
arr.push_back(5);
arr.push_back(10);
arr.push_back(1);

int sum = 0
sum = arr.end()[-1] + arr.end()[-2];


Last edited on
You can use a vector like an array:

sum = arr[1] + arr[3];

This adds the second and the third element of the vector.
Topic archived. No new replies allowed.