Sortowanie wektora struktur z referencjami

0
#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?

1

zdefiniowałeś operator przypisania dla sint_ref, który teraz nic nie robi (WTF), więc sortowanie nie jest w stanie przestawiać elementów.

Zmień pole referencje wewnątrz klasy na wskaźnik wywal ten operator przypisania to będzie wtedy działać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1