Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- using std::vector;
- using std::cout;
- using std::endl;
- int sum(vector<int> &);
- int main() {
- vector<int> vec {1, 2, 3};
- // Pass the reference of vec into sum
- int s = sum(vec);
- cout << s << endl;
- cout << vec[1] << endl;
- return 0;
- }
- int sum(vector<int> &vec) {
- int sum = 0;
- for (auto ptr = vec.begin(); ptr != vec.end(); ptr++) {
- sum += *ptr;
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement