#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct sint
{
sint(int i) : s(i) { }
bool operator<(const sint &a)
{
return s < a.s;
}
int s;
};
struct sint_ref
{
sint_ref(int &i) : s(i) { }
bool operator<(const sint_ref &a)
{
return s < a.s;
}
sint_ref& operator=(sint_ref &&rhs)
{
return *this;
}
int &s;
};
int main()
{
//******************************************
cout << "PRZYPADEK 1" << endl;
vector<sint> sints;
sints.push_back(sint(5));
sints.push_back(sint(0));
sints.push_back(sint(1));
sort(sints.begin(), sints.end());
for_each(sints.begin(), sints.end(), [] (sint &s) {
cout << s.s << endl;
});
//******************************************
cout << endl;
//******************************************
cout << "PRZYPADEK 2" << endl;
int a = 5;
int b = 0;
int c = 1;
vector<sint_ref> sints_ref;
sints_ref.push_back(sint_ref(a));
sints_ref.push_back(sint_ref(b));
sints_ref.push_back(sint_ref(c));
sort(sints_ref.begin(), sints_ref.end());
for_each(sints_ref.begin(), sints_ref.end(), [] (sint_ref &s) {
cout << s.s << endl;
});
//******************************************
return 0;
}
W przypadku 1. wszystko jest ok.
W przypadku 2. wektor nie jest sortowany.
Dlaczego tak się dzieje?