包含测试 [英] Test for inclusion

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

问题描述

我有两个已分类的容器,我想知道第二个容器中是否存在来自一个

容器的任何项目。

标准库中有什么要做的吗?


谢谢

I have two sorted containers, and I wish to know if any items from one
container exist in the second container. Is there anything in the
standard library to do this?

Thanks

推荐答案

set_intersection怎么样?

http://www.sgi.com/tech/stl/set_intersection.html

Moohoo写道:
How about set_intersection?

http://www.sgi.com/tech/stl/set_intersection.html

Moohoo wrote:

我有两个已分类的容器,我想知道第二个容器中是否存在来自一个

容器的任何项目。

标准库中有什么要做的吗?


谢谢
I have two sorted containers, and I wish to know if any items from one
container exist in the second container. Is there anything in the
standard library to do this?

Thanks


Moohoo写道:
Moohoo wrote:

我有两个已分类的容器,我想知道第二个容器中是否有来自一个

容器的任何物品。

标准库中有什么可以做到的吗?
I have two sorted containers, and I wish to know if any items from one
container exist in the second container. Is there anything in the
standard library to do this?



您可以使用std :: set_intersection()。如果你只对

的交集感兴趣,你可以使用一个output_iterator,当std :: set_intersection()写入第一个元素时,它会立即抛出




#include< set>

#include< algorithm>

#include< iterator>


模板< typename T>

struct throwing_iterator

:public std :: iterator< std :: output_iterator_tag,void,void,void,void>

{


struct flag_type {};


throw_iterator& operator =(T const& value){

throw(flag_type());

}


throwing_iterator& operator *(void){

return(* this);

}


throwing_iterator& operator ++(void){

return(* this);

}


throwing_iterator& operator ++(int){

return(* this);

}


}; // throwing_iterator

template< typename Iterator>

bool has_intersection(Iterator from1,Iterator to1,

Iterator from2,Iterator to2){

typedef typename std :: iterator_traits< ; Iterator> :: value_type value_type;

throwing_iterator< value_type t_iter;

尝试{

std :: set_intersection(from1,to1,from2,to2,t_iter);

}

catch(typename throwing_iterator< value_type> :: flag_type){

return(true);

}

return(false );

}


请注意,这有点不正统。

更传统的方法是迭代一个范围并使用一个

的各种二进制搜索方法(std :: lower_bound(),std :: upper_bound,

std :: binary_search())来查找元素另一个范围。

最好


Kai-Uwe Bux

You could use std::set_intersection(). If you are only interested in whether
the intersection is empty, you could use an output_iterator that throws as
soon as the std::set_intersection() writes the first element:

#include <set>
#include <algorithm>
#include <iterator>

template < typename T >
struct throwing_iterator
: public std::iterator< std::output_iterator_tag, void, void, void, void >
{

struct flag_type {};

throwing_iterator & operator= ( T const & value ) {
throw ( flag_type() );
}

throwing_iterator & operator* ( void ) {
return ( *this );
}

throwing_iterator & operator++ ( void ) {
return ( *this );
}

throwing_iterator & operator++ ( int ) {
return ( *this );
}

}; // throwing_iterator
template < typename Iterator >
bool has_intersection ( Iterator from1, Iterator to1,
Iterator from2, Iterator to2 ) {
typedef typename std::iterator_traits< Iterator >::value_type value_type;
throwing_iterator< value_type t_iter;
try {
std::set_intersection( from1, to1, from2, to2, t_iter );
}
catch ( typename throwing_iterator< value_type >::flag_type ) {
return ( true );
}
return ( false );
}

Note that this is a little unorthodox.
An more conventional method would be to iterate over one range and use one
of the various binary search methods (std::lower_bound(), std::upper_bound,
std::binary_search()) to find the element in the other range.
Best

Kai-Uwe Bux


我不知道如果我鼓励使用例外 - 当然

空交叉口不是特殊状态,除非你在没有检查它是否包含任何内容的情况下访问它。


您可以制作一个临时容器,看看它被放入

吧。如果您持有非常大的对象,也许您可​​以将它们放在一个带有boost :: shared_ptr的容器中,以防止将一个大对象复制到一个临时集合中 -
只复制一个shared_ptr。


下限的东西也可以。


-J


Kai-Uwe Bux写道:
I don''t know if I would encourage this use of an exception - certainly
an empty intersection is not an exceptional state, unless you were
accessing it without checking to see if it contained anything.

You could just make a temporary container and see what gets put into
it. If you are holding very large objects, perhaps you could put them
in a container with boost::shared_ptr to prevent copying a large object
to a temporary set - you would only copy a shared_ptr.

The lower-bound thing would work, also.

-J

Kai-Uwe Bux wrote:

Moohoo写道:
Moohoo wrote:

我有两个已分类的容器,并且我想知道第二个容器中是否存在来自一个

容器的任何物品。

标准库中有什么可以做到的吗?
I have two sorted containers, and I wish to know if any items from one
container exist in the second container. Is there anything in the
standard library to do this?



您可以使用std :: set_intersection()。如果你只对

的交集感兴趣,你可以使用一个output_iterator,当std :: set_intersection()写入第一个元素时,它会立即抛出




#include< set>

#include< algorithm>

#include< iterator>


模板< typename T>

struct throwing_iterator

:public std :: iterator< std :: output_iterator_tag,void,void,void,void>

{


struct flag_type {};


throw_iterator& operator =(T const& value){

throw(flag_type());

}


throwing_iterator& operator *(void){

return(* this);

}


throwing_iterator& operator ++(void){

return(* this);

}


throwing_iterator& operator ++(int){

return(* this);

}


}; // throwing_iterator


模板< typename Iterator>

bool has_intersection(Iterator from1,Iterator to1,

Iterator from2,Iterator to2){

typedef typename std :: iterator_traits< ; Iterator> :: value_type value_type;

throwing_iterator< value_type t_iter;

尝试{

std :: set_intersection(from1,to1,from2,to2,t_iter);

}

catch(typename throwing_iterator< value_type> :: flag_type){

return(true);

}

return(false );

}


请注意,这有点不正统。


更常规的方法是迭代一个范围并使用一个

的各种二进制搜索方法(std :: lower_bound(),std :: upper_bound,

std :: binary_search())来找到其他范围内的元素。


最好


Kai-Uwe Bux


You could use std::set_intersection(). If you are only interested in whether
the intersection is empty, you could use an output_iterator that throws as
soon as the std::set_intersection() writes the first element:

#include <set>
#include <algorithm>
#include <iterator>

template < typename T >
struct throwing_iterator
: public std::iterator< std::output_iterator_tag, void, void, void, void >
{

struct flag_type {};

throwing_iterator & operator= ( T const & value ) {
throw ( flag_type() );
}

throwing_iterator & operator* ( void ) {
return ( *this );
}

throwing_iterator & operator++ ( void ) {
return ( *this );
}

throwing_iterator & operator++ ( int ) {
return ( *this );
}

}; // throwing_iterator
template < typename Iterator >
bool has_intersection ( Iterator from1, Iterator to1,
Iterator from2, Iterator to2 ) {
typedef typename std::iterator_traits< Iterator >::value_type value_type;
throwing_iterator< value_type t_iter;
try {
std::set_intersection( from1, to1, from2, to2, t_iter );
}
catch ( typename throwing_iterator< value_type >::flag_type ) {
return ( true );
}
return ( false );
}

Note that this is a little unorthodox.
An more conventional method would be to iterate over one range and use one
of the various binary search methods (std::lower_bound(), std::upper_bound,
std::binary_search()) to find the element in the other range.
Best

Kai-Uwe Bux


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

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