
10.10.2022, 19:47
|
|
Постоянный
Регистрация: 15.12.2013
Сообщений: 412
С нами:
6530249
Репутация:
133
|
|
Сообщение от rinkу
17 строка, как сделать вызов в таком виде как в коменте? что изменить в структуре для этого
Через стандартное наследование, это первое что в голову приходит
C++:
Код:
template
class
VecExt
:
public
std
::
vector
{
public
:
VecExt
(
std
::
initializer_list
l
)
:
std
::
vector
(
l
)
{
}
std
::
string
to_string
(
)
const
{
std
::
stringstream temp
;
std
::
copy
(
this
->
begin
(
)
,
this
->
end
(
)
,
std
::
ostream_iterator
(
temp
,
" "
)
)
;
return
temp
.
str
(
)
;
}
}
;
Но вообще можно конкретно для i/o потоков и для вашего примера указать реализацию оператора
C++:
Код:
friend
std
::
ostream
&
operator
&
m
)
{
std
::
copy
(
m
.
begin
(
)
,
m
.
end
(
)
,
std
::
ostream_iterator
(
os
,
" "
)
)
;
return
os
;
}
Полный код:
Сообщение от Спойлер
C++:
[CODE]
#include
#include
#include
#include
#include
#include
template
class
VecExt
:
public
std
::
vector
{
public
:
VecExt
(
std
::
initializer_list
l
)
:
std
::
vector
(
l
)
{
}
std
::
string
to_string
(
)
const
{
std
::
stringstream temp
;
std
::
copy
(
this
->
begin
(
)
,
this
->
end
(
)
,
std
::
ostream_iterator
(
temp
,
" "
)
)
;
return
temp
.
str
(
)
;
}
friend
std
::
ostream
&
operator
&
m
)
{
std
::
copy
(
m
.
begin
(
)
,
m
.
end
(
)
,
std
::
ostream_iterator
(
os
,
" "
)
)
;
return
os
;
}
}
;
int
main
(
)
{
const
auto
v
=
VecExt
{
1
,
3
,
5
,
4
}
;
std
::
cout
|
|
|