Predykat sortowania STL set

0

Jak sortować wzgledem strPhoneNumber w ponizszym przypadku?

struct ContactItem
{
	string strContactsName;
	string strPhoneNumber;

        ContactItem(const string& strName, const string & strNumber)
	{
		strContactsName = strName;
		strPhoneNumber = strNumber;
	}

	bool operator == (const ContactItem& itemToCompare) const
	{
		return (itemToCompare.strContactsName == this->strContactsName);
	}

	bool operator < (const ContactItem& itemToCompare) const
	{
		return (this->strContactsName < itemToCompare.strContactsName);
	}

};

void FindContact(const set <ContactItem>& setContacts);
void FindContactByNumber(const set <ContactItem>& setContacts);
 
 void FindContact(const set <ContactItem>& setContacts)
{
	cout << "*** Wyszukanie danych kontaktowych ***" << endl;
	cout << "Czyj numer telefonu chcialbys znalezc?" << endl;
	cout << "> ";
	string strName;
	cin >> strName;

	set <ContactItem>::const_iterator iContactFound
		= setContacts.find(ContactItem(strName, ""));

	if (iContactFound != setContacts.end())
	{
		cout << strName << " jest dostepny pod numerem telefonu: ";
		cout << iContactFound->strPhoneNumber << endl;
	}
	else
		cout << strName << " nie zostal znaleziony na liscie kontaktow" << endl;

	cout << endl;

	return;
}
void FindContactByNumber(const set <ContactItem>& setContacts)
{
	
	cout << "Znajdz imie kontaktu po numerze telefonu!" << endl;
	cout << "Wpisz numer telefonu" << endl;
	cout << ">";
	string strPhoneNumber;
	cin >> strPhoneNumber;

	set <ContactItem>::const_iterator iContactFound
		= setContacts.find(ContactItem("", strPhoneNumber));

	if (iContactFound != setContacts.end())
	{
		cout << strPhoneNumber << " jest numerem telefonu do: ";
		cout << iContactFound->strContactsName << endl;
	}
	else
	{
		cout << "Kontakt o podanym numerze telefonu nie zostal znaleziony!" << endl;
	}

} 

W funkcji FindContactByNumber muszę dodać predykat sortowania, który przesłoni operator < i domyślne sortowanie na nim bazujące lecz nie wiem jak to zrobić.

0

Może find_if?

Wtedy funktor:

 class FindByNumber
{
	string number;
public:
	FindByNumber(string _number):number(_number) { }
	bool operator() (const ContactItem& it) { return it.strPhoneNumber==number; }
};

Wyszukiwanie:

void FindContactByNumber(const set <ContactItem>& setContacts)
{
    cout << "Znajdz imie kontaktu po numerze telefonu!" << endl;
    cout << "Wpisz numer telefonu" << endl;
    cout << ">";
    string strPhoneNumber;
    cin >> strPhoneNumber;
 
    FindByNumber pred(strPhoneNumber);
    auto iContactFound = find_if(setContacts.begin(), setContacts.end(), pred);
    
    if (iContactFound != setContacts.end())
    {
        cout << strPhoneNumber << " jest numerem telefonu do: ";
        cout << iContactFound->strContactsName << endl;
    }
    else
        cout << "Kontakt o podanym numerze telefonu nie zostal znaleziony!" << endl;
} 

http://ideone.com/sqzNwi

Tak teraz myślę, czy warto tak to komplikować skoro algorytm find_if i tak ma złożoność liniową i uruchamia predykat dla każdego elementu.
Więc może po prostu:

void FindContactByNumber(const set <ContactItem>& setContacts)
{
 
    cout << "Znajdz imie kontaktu po numerze telefonu!" << endl;
    cout << "Wpisz numer telefonu" << endl;
    cout << ">";
    string strPhoneNumber;
    cin >> strPhoneNumber;
 
 	for (auto it=setContacts.begin(); it!=setContacts.end(); ++it)
 	{
 		if (it->strPhoneNumber==strPhoneNumber)
 			cout << "Numer nalezy do " << it->strContactsName << endl;
	}
} 

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