I try to modify vector value use pass by reference in following code.
#include <iostream>
#include <vector>
void func(int a, int b, int& c) {
c = a + b;
}
int main() {
std::vector<int> v = {1, 2, 3, 4};
func(3, 4, v[0]); // is this valid???
for(int i : v) {
std::cout << i << " ";
}
// Output be: 7 2 3 4
return 0;
}
When I ask some LLM models like gpt3.5, claude, it says this code is incorrect,but I can get the right answer when I actually run the code. By the way, GPT4o says this is correct. Can someone tells me what’s going on here?
New contributor
Jay Lin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.