
05.10.2022, 00:34
|
|
Постоянный
Регистрация: 16.08.2020
Сообщений: 553
С нами:
3022568
Репутация:
68
|
|
Сообщение от kin4stat
Для std::set нужны оператор сравнения для сортировки.
Решение - использовать std::unordered_set, или написать операторы сравнения(==;!=;
я этот код сократил и кинул только нужное для понимания, дело в не в этом, оно почему то не может использовать значение по ссылке, когда убираю & - программа компилируется
C++:
Код:
struct
Contact
{
string name
,
phone
,
namePlusPhone
;
Contact
(
string
&
_name
,
string
&
_phone
)
:
name
(
_name
)
,
phone
(
_phone
)
,
namePlusPhone
(
_name
+
": "
+
_phone
)
{
}
;
operator
const
char
*
(
)
const
{
return
namePlusPhone
.
c_str
(
)
;
}
bool
operator
==
(
const
Contact
&
item
)
const
{
return
(
item
.
name
==
this
->
name
)
;
}
bool
operator
this
->
name
)
;
}
}
;
int
main
(
)
{
set
contacts
;
contacts
.
insert
(
Contact
(
"Jack Nilson"
,
"+841414455"
)
)
;
contacts
.
insert
(
Contact
(
"Hardi Jel"
,
"+311414141"
)
)
;
contacts
.
insert
(
Contact
(
"Andy Kil"
,
"+42523412"
)
)
;
contacts
.
insert
(
Contact
(
"Leon Brawl"
,
"+532342342342"
)
)
;
contacts
.
insert
(
Contact
(
"Jackson Will"
,
"+32466666621"
)
)
;
contacts
.
insert
(
Contact
(
"Andrew Redmond"
,
"+41414345346"
)
)
;
contacts
.
insert
(
Contact
(
"Julia Breed"
,
"+54312346236"
)
)
;
}
|
|
|