back_insert_iterator的value_type [英] value_type of a back_insert_iterator

查看:63
本文介绍了back_insert_iterator的value_type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么back_insert_iterator的类型为void而不是其封装容器的类型?


为什么:


namespace std {


template< class Category,class T,class Distance = ptrdiff_t,

class Pointer = T *,class Reference = T& > struct

iterator;


模板< class Container>

class back_insert_iterator:

public iterator< ; output_iterator_tag,空隙,空隙,空隙,空隙> {

受保护:

容器*容器;

// - 为清晰起见而移除

};

}


而不是:


模板< class Container>

class back_insert_iterator :

public iterator< output_iterator_tag,typename

Container :: value_type,void,void,void> {

受保护:

容器*容器;

// - 为清晰起见而移除

};

解决方案

" Chyi Pin Lim" < NI ***** @ hotmail.com>在消息中写道

news:c9 ************************** @ posting.google.c om ...

为什么back_insert_iterator的类型为void而不是其封装容器的类型?




因为它这是一个输出迭代器,它是一个纯粹的接收器。 (你不能瞥见它控制的序列中存储的任何东西。)


PJ Plauger

Dinkumware,Ltd。
http://www.dinkumware.com


" PJ Plauger" < pj*@dinkumware.com>在消息新闻中写道:< kA *************** @ nwrddc01.gnilink.net> ...

" Chyi Pin Lim" < NI ***** @ hotmail.com>在消息中写道
新闻:c9 ************************** @ posting.google.c om ...

为什么back_insert_iterator的类型为void而不是其封装容器的类型?



因为它是一个输出迭代器,是一个纯粹的水槽。 (你不能偷看它控制的序列中存储的任何东西。)

PJ Plauger
Dinkumware,Ltd。
http://www.dinkumware.com




确实我不能偷看,但是在插入一个值时,如果有人想要使用参数化的话,知道这种类型是不是很有用

构造函数/强制参数化构造。


我的意思如下:


//跟随std :: copy

模板< typename InIt,typename OutIt>

OutIt copy_to_container(InIt begin,InIt end,OutIt result)

{

//想要使用value_type输出

* result = OutIt :: value_type(开始,结束);

返回++结果;

}


使用追加:


char s [] =" abc";

vector< string> VS(1); //包含1个字符串

vector< vector< char> > vvc(1);


copy_to_container(s,s + 1,vs.begin()); // ok

copy_to_container(s,s + 1,vvc.begin()); // ok

copy_to_container(s,s + 2,back_inserter(vs)); //无法编译

如果back_inserter的value_type被定义为其封装容器的类型,我们可以使用该属性。


由于这是不可能的,请告知其他

实施的灵活性。


" Chyi Pin Lim" < NI ***** @ hotmail.com>在消息中写道

news:c9 ************************** @ posting.google.c om ...

" PJ Plauger" < pj*@dinkumware.com>写在留言
news:< kA *************** @ nwrddc01.gnilink.net> ...

" Chyi Pin Lim" < NI ***** @ hotmail.com>在消息中写道
新闻:c9 ************************** @ posting.google.c om ...

为什么back_insert_iterator的类型为void而不是其封装容器的类型?



因为它是一个输出迭代器,是一个纯粹的水槽。 (你不能偷看它控制的序列中存储的任何东西。)

PJ Plauger
Dinkumware,Ltd。
http://www.dinkumware.com



我是真的不能偷看,但在插入一个值时,如果有人想要使用参数化的构造函数/强制参数化的构造,知道类型是不是有益。




可能是,但这不是输出迭代器的要求。

我的意思如下:

/ /跟随std :: copy
模板< typename InIt,typename OutIt>
OutIt copy_to_container(InIt begin,InIt end,OutIt result)
//
//想要使用value_type输出
* result = OutIt :: value_type(开始,结束);


这个地方不应该有循环吗?

返回++结果;
}

使用追加:

char s [] =" abc" ;;
vector< string> VS(1); //包含1个字符串
vector< vector< char> > vvc(1);

copy_to_container(s,s + 1,vs.begin()); // ok
copy_to_container(s,s + 1,vvc.begin()); // ok
copy_to_container(s,s + 2,back_inserter(vs)); //无法编译

如果back_inserter的value_type被定义为其封装容器的类型,我们可以使用该属性。

因为这不是可能,请建议灵活的替代
实施。




如果你想使用其他类型信息来约束

对输出迭代器的赋值,你必须通过不同的频道提供它。



PJ Plauger

Dinkumware ,有限公司
http://www.dinkumware.com


Why is the type for back_insert_iterator void and not the type of its
encapsulating container?

Why:

namespace std {

template<class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&> struct
iterator;

template <class Container>
class back_insert_iterator :
public iterator<output_iterator_tag,void,void,void,void> {
protected:
Container* container;
// -- removed for clarity
};
}

Instead of:

template <class Container>
class back_insert_iterator :
public iterator<output_iterator_tag, typename
Container::value_type,void,void,void> {
protected:
Container* container;
// -- removed for clarity
};

解决方案

"Chyi Pin Lim" <ni*****@hotmail.com> wrote in message
news:c9**************************@posting.google.c om...

Why is the type for back_insert_iterator void and not the type of its
encapsulating container?



Because it''s an output iterator, which is a pure sink. (You can''t
peek at any of the things stored in the sequence it controls.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


"P.J. Plauger" <pj*@dinkumware.com> wrote in message news:<kA***************@nwrddc01.gnilink.net>...

"Chyi Pin Lim" <ni*****@hotmail.com> wrote in message
news:c9**************************@posting.google.c om...

Why is the type for back_insert_iterator void and not the type of its
encapsulating container?



Because it''s an output iterator, which is a pure sink. (You can''t
peek at any of the things stored in the sequence it controls.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



True that I can''t peek, but in inserting a value, wouldn''t it be
beneficial to know the type in case someone wants to use parameterized
constructor / enforce parameterized contruction.

What I meant is as follows:

// following std::copy
template <typename InIt, typename OutIt>
OutIt copy_to_container(InIt begin, InIt end, OutIt result)
{
// want to use value_type for output
*result = OutIt::value_type(begin, end);
return ++result;
}

Using append:

char s[] = "abc";
vector<string> vs(1); // contain 1 string
vector<vector<char> > vvc(1);

copy_to_container(s, s+1, vs.begin()); // ok
copy_to_container(s, s+1, vvc.begin()); // ok
copy_to_container(s, s+2, back_inserter(vs)); // fail to compile
If the value_type of back_inserter is defined as the type of its
encapsulating container, we can utilize that property.

Since this is not possible, please advise on alternative
implementation that is as flexible.


"Chyi Pin Lim" <ni*****@hotmail.com> wrote in message
news:c9**************************@posting.google.c om...

"P.J. Plauger" <pj*@dinkumware.com> wrote in message news:<kA***************@nwrddc01.gnilink.net>...

"Chyi Pin Lim" <ni*****@hotmail.com> wrote in message
news:c9**************************@posting.google.c om...

Why is the type for back_insert_iterator void and not the type of its
encapsulating container?



Because it''s an output iterator, which is a pure sink. (You can''t
peek at any of the things stored in the sequence it controls.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



True that I can''t peek, but in inserting a value, wouldn''t it be
beneficial to know the type in case someone wants to use parameterized
constructor / enforce parameterized contruction.



It might be, but that''s not a requirement for output iterators.
What I meant is as follows:

// following std::copy
template <typename InIt, typename OutIt>
OutIt copy_to_container(InIt begin, InIt end, OutIt result)
{
// want to use value_type for output
*result = OutIt::value_type(begin, end);
Shouldn''t there be a loop in here somewhere?
return ++result;
}

Using append:

char s[] = "abc";
vector<string> vs(1); // contain 1 string
vector<vector<char> > vvc(1);

copy_to_container(s, s+1, vs.begin()); // ok
copy_to_container(s, s+1, vvc.begin()); // ok
copy_to_container(s, s+2, back_inserter(vs)); // fail to compile
If the value_type of back_inserter is defined as the type of its
encapsulating container, we can utilize that property.

Since this is not possible, please advise on alternative
implementation that is as flexible.



If you want to use additional type information to constrain
assignments to an output iterator, you''ll have to supply it
by a different channel.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


这篇关于back_insert_iterator的value_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆