Today i was playing around with some standard Library functions.. and found this weird output from std::transform() and std::back_inserter whenever the container am using(in this case vector) has more than 2 elements in it.I don't understand this kind of behavior can anyone there help me...
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
/* this version of change works fine for me when i store the incoming value into a
static variable
int change(int n){
static int m=n;
return m * m;
}*/
int change(int n){
return n*n;
}
int main(){
vector<int> v2(3,3),
v1;
transform(v2.begin(),v2.end(),back_inserter(v2),change);
for(auto v: v2)
{
cout << v <<" "; // prints out a strange random 5th value in v2.
}
return 0;
}