具有不同比较器的集合具有相同的迭代器类型;标准? [英] sets with different comparators have same iterator type; standard?

查看:56
本文介绍了具有不同比较器的集合具有相同的迭代器类型;标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是以下代码标准? (在g ++ - 4.0.2下干净地编译):


struct Asc {bool operator()(int a,int b){return a< b;}};

struct Des {bool operator()(int a,int b){return b> a;}};

int main(){

int arr [] = {1,2,3};

set< int ,ASC> asc(arr,arr + 3);

set< int,Des> :: iterator beg = asc.begin(); // [*]

set< int,Des> :: iterator end = asc.end(); // [*]

copy(beg,end,ostream_iterator< int>(cout,"")); //打印123

返回0;

}


请注意作业双方的_sets_类型[*由于它们的比较器不同(Asc / Des),因此b $ b不同。尽管如此,[*]似乎是标准的,因为迭代器的模板参数

是''Key''和''Distance''(不是''Comparator''),所以迭代器

类型是平等的,对吗? [我们使用用户定义的模板''比较器''

参数(Des / Asc),所以他们没有标准的专业化,

这意味着我们可以肯定两个迭代器的距离类型

是相同的; ''Key''类型(int)也明显相同]。


谢谢,

--dan

is the following code standard? (cleanly compiles under g++-4.0.2):

struct Asc { bool operator()(int a, int b) {return a < b;} };
struct Des { bool operator()(int a, int b) {return b > a;} };
int main() {
int arr[] = {1, 2, 3};
set<int,Asc> asc(arr, arr+3);
set<int,Des>::iterator beg = asc.begin(); //[*]
set<int,Des>::iterator end = asc.end(); //[*]
copy(beg, end, ostream_iterator<int>(cout, "")); // prints 123
return 0;
}

note that the types of the _sets_ in both sides of assignment[*] are
different due to their different comparators (Asc/Des). nevertheless,[*] seems to be standard as the template parameters of the iterator
are ''Key'' and ''Distance'' (not the ''Comparator''), and so the iterator
types are equal, right? [we use user defined template ''Comparator''
parameters (Des/Asc), so they don''t have standard specialization,
which means we can be sure the ''Distance'' type of the two iterators
is the same; the ''Key'' type (int) is also obviously the same].

thanks,
--dan

推荐答案

Dan Tsafrir写道:
Dan Tsafrir wrote:
是以下代码标准? (在g ++ - 4.0.2下干净地编译):

struct Asc {bool operator()(int a,int b){return a< b;}};
struct Des {bool operator()(int a,int b){return b> a;}};
int main(){
int arr [] = {1,2,3};
set< int,Asc> asc(arr,arr + 3);
set< int,Des> :: iterator beg = asc.begin(); // [*]
set< int,Des> :: iterator end = asc.end(); // [*]
copy(beg,end,ostream_iterator< int>(cout,"")); //打印123
返回0;
}
请注意,作业[*]两侧的_sets_类型因其不同的比较器而不同(ASC / DES)。尽管如此,
[*]似乎是标准的,因为迭代器的模板参数是''Key''和''Distance''(而不是''Comparator''),因此迭代器
类型是平等的吧? [我们使用用户定义的模板''比较器''
参数(Des / Asc),所以他们没有标准的专业化,这意味着我们可以确定''距离''类型这两个迭代器是一样的; ''Key''类型(int)也明显相同]。

谢谢,
- 丹
is the following code standard? (cleanly compiles under g++-4.0.2):

struct Asc { bool operator()(int a, int b) {return a < b;} };
struct Des { bool operator()(int a, int b) {return b > a;} };
int main() {
int arr[] = {1, 2, 3};
set<int,Asc> asc(arr, arr+3);
set<int,Des>::iterator beg = asc.begin(); //[*]
set<int,Des>::iterator end = asc.end(); //[*]
copy(beg, end, ostream_iterator<int>(cout, "")); // prints 123
return 0;
}

note that the types of the _sets_ in both sides of assignment[*] are
different due to their different comparators (Asc/Des). nevertheless,
[*] seems to be standard as the template parameters of the iterator
are ''Key'' and ''Distance'' (not the ''Comparator''), and so the iterator
types are equal, right? [we use user defined template ''Comparator''
parameters (Des/Asc), so they don''t have standard specialization,
which means we can be sure the ''Distance'' type of the two iterators
is the same; the ''Key'' type (int) is also obviously the same].

thanks,
--dan




这段代码很好。 std :: copy不要求两个迭代器是相同类型的
。事实上,他们甚至不需要属于那些

保持相同类型的容器 - 只要被复制的类型可以转换为输出迭代器的类型。

所以在这个例子中,你可以复制一组< int>到一组< long>甚至是

设置< double>还是没问题。


Greg



This code is fine. std::copy does not require that the two iterators be
of the same type. In fact, they need not even belong to containers that
hold identical types - as long as the type being copied is convertible
to the output iterator''s type.

So in this example, you could copy a set<int> to a set<long> or even a
set<double> and still be OK.

Greg


Dan Tsafrir写道:
Dan Tsafrir wrote:
是以下代码标准? (在g ++ - 4.0.2下干净地编译):

struct Asc {bool operator()(int a,int b){return a< b;}};
struct Des {bool operator()(int a,int b){return b> a;}};
int main(){
int arr [] = {1,2,3};
set< int,Asc> asc(arr,arr + 3);
set< int,Des> :: iterator beg = asc.begin(); // [*]
set< int,Des> :: iterator end = asc.end(); // [*]
copy(beg,end,ostream_iterator< int>(cout,"")); //打印123
返回0;
}
is the following code standard? (cleanly compiles under g++-4.0.2):

struct Asc { bool operator()(int a, int b) {return a < b;} };
struct Des { bool operator()(int a, int b) {return b > a;} };
int main() {
int arr[] = {1, 2, 3};
set<int,Asc> asc(arr, arr+3);
set<int,Des>::iterator beg = asc.begin(); //[*]
set<int,Des>::iterator end = asc.end(); //[*]
copy(beg, end, ostream_iterator<int>(cout, "")); // prints 123
return 0;
}




注意1:您的Asc和Des仿函数具有相同的行为。


注2:Asc和Des中的operator()应该是const方法,并且

Asc和Desc应该可以从std :: binary_function<>继承。
<例如:


#include< functional>

struct Asc:public std :: binary_function< int, int,bool>

{

bool operator()(int a,int b)const {return a< b; }

}


struct Des:public std :: binary_function< int,int,bool>

{

//假设你不想要相同的行为

// Asc和Des

bool operator()(int a,int b)const {返回> b; }

}



Note 1: You Asc and Des functors have identical behavior.

Note 2: operator() in Asc and Des should be const methods, and
Asc and Desc should probably inherit from std::binary_function<>.

e.g.:

#include <functional>

struct Asc: public std::binary_function<int,int,bool>
{
bool operator()(int a, int b) const { return a < b; }
}

struct Des: public std::binary_function<int,int,bool>
{
// assumes you didn''t want identical behavior for
// Asc and Des
bool operator()(int a, int b) const { return a > b; }
}


Greg写道:
Dan Tsafrir写道:
Dan Tsafrir wrote:
是以下代码标准? (在g ++ - 4.0.2下干净地编译):

struct Asc {bool operator()(int a,int b){return a< b;}};
struct Des {bool operator()(int a,int b){return b> a;}};
int main(){
int arr [] = {1,2,3};
set< int,Asc> asc(arr,arr + 3);
set< int,Des> :: iterator beg = asc.begin(); // [*]
set< int,Des> :: iterator end = asc.end(); // [*]
copy(beg,end,ostream_iterator< int>(cout,"")); //打印123
返回0;
}
is the following code standard? (cleanly compiles under g++-4.0.2):

struct Asc { bool operator()(int a, int b) {return a < b;} };
struct Des { bool operator()(int a, int b) {return b > a;} };
int main() {
int arr[] = {1, 2, 3};
set<int,Asc> asc(arr, arr+3);
set<int,Des>::iterator beg = asc.begin(); //[*]
set<int,Des>::iterator end = asc.end(); //[*]
copy(beg, end, ostream_iterator<int>(cout, "")); // prints 123
return 0;
}



此代码没问题。 std :: copy不要求两个迭代器是相同类型的。实际上,它们甚至不需要属于具有相同类型的容器 - 只要被复制的类型可以转换为输出迭代器的类型。

所以在此示例中,您可以复制一组< int>到一组< long>甚至是
设置< double>并且仍然可以。


This code is fine. std::copy does not require that the two iterators be
of the same type. In fact, they need not even belong to containers that
hold identical types - as long as the type being copied is convertible
to the output iterator''s type.

So in this example, you could copy a set<int> to a set<long> or even a
set<double> and still be OK.




问题不是关于copy(),而是关于[*](副本()就在那里

使程序有意义)。 [*]的问题是程序

为类型的对象设置类型的对象:set< int,asc> :: iterator

:set< ; int,Dec> :: iterator


谢谢,

--dan



the question is not about copy(), it''s about[*] (the copy() is just there
to make the program meaningful). the problem with[*] is that the program
assigns an object of the type: set<int,Asc>::iterator
to an object of the type : set<int,Dec>::iterator

thanks,
--dan


这篇关于具有不同比较器的集合具有相同的迭代器类型;标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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