Understand how the code works

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    vector<int> vec = {1, 2, 3, 4};
    // since c++17, can be simplified by using `auto`
    const vector<int>::iterator itr = find(vec.begin(), vec.end(), 2);
    if (itr != vec.end())
    {
        *itr = 3;
    }
    if (const vector<int>::iterator itr = find(vec.begin(), vec.end(), 3);
    itr != vec.end())
    {
        *itr = 4;
    }
    // should output: 1, 4, 3, 4. can be simplified using `auto`
    for (vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
    cout << *element << endl;
}

:balloon: Hi, welcome to the forum!
We see you have posted some code but did you have a question?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.