1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Just for output
template<typename T> ostream & operator << ( ostream &out, const vector<T> &V )
{
for ( T e : V ) out << e << " ";
return out;
}
int main()
{
vector<int> A = { 7, 8, 5, 9, 10, 5, 11, 12, 5, 10, 11, 12 };
cout << A << '\n';
int target = 5;
int changeTo = 100;
auto it = find( A.rbegin(), A.rend(), target ); // search BACKWARDS
if ( it == A.rend() )
{
cout << "Not found\n";
}
else
{
*it = changeTo;
cout << A << '\n';
}
}
|