使用带有 set_intersection 的地图
以前没有使用过 set_intersection,但我相信它可以与地图一起使用。我编写了以下示例代码,但它没有给我所期望的结果:
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
struct Money
{
double amount;
string currency;
bool operator< ( const Money& rhs ) const
{
if ( amount != rhs.amount )
return ( amount < rhs.amount );
return ( currency < rhs.currency );
}
};
int main( int argc, char* argv[] )
{
Money mn[] =
{
{ 2.32, "USD" },
{ 2.76, "USD" },
{ 4.30, "GBP" },
{ 1.21, "GBP" },
{ 1.37, "GBP" },
{ 6.74, "GBP" },
{ 2.55, "EUR" }
};
typedef pair< int, Money > MoneyPair;
typedef map< int, Money > MoneyMap;
MoneyMap map1;
map1.insert( MoneyPair( 1, mn[0] ) );
map1.insert( MoneyPair( 2, mn[1] ) );
map1.insert( MoneyPair( 3, mn[2] ) ); // (3)
map1.insert( MoneyPair( 4, mn[3] ) ); // (4)
MoneyMap map2;
map2.insert( MoneyPair( 3, mn[2] ) ); // (3)
map2.insert( MoneyPair( 4, mn[3] ) ); // (4)
map2.insert( MoneyPair( 5, mn[4] ) );
map2.insert( MoneyPair( 6, mn[5] ) );
map2.insert( MoneyPair( 7, mn[6] ) );
MoneyMap out;
MoneyMap::iterator out_itr( out.begin() );
set_intersection( map1.begin(), map1.end(), map2.begin(), map2.end(), inserter( out, out_itr ) );
cout << "intersection has " << out.size() << " elements." << endl;
return 0;
}
由于标记为 (3) 和 (4) 的对出现在两个映射中,我期望在交集中得到 2 个元素,但是不,我明白了:
intersection has 0 elements.
我确信这与地图/对上的比较器有关,但无法弄清楚。
Not used set_intersection before, but I believe it will work with maps. I wrote the following example code but it doesn't give me what I'd expect:
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
struct Money
{
double amount;
string currency;
bool operator< ( const Money& rhs ) const
{
if ( amount != rhs.amount )
return ( amount < rhs.amount );
return ( currency < rhs.currency );
}
};
int main( int argc, char* argv[] )
{
Money mn[] =
{
{ 2.32, "USD" },
{ 2.76, "USD" },
{ 4.30, "GBP" },
{ 1.21, "GBP" },
{ 1.37, "GBP" },
{ 6.74, "GBP" },
{ 2.55, "EUR" }
};
typedef pair< int, Money > MoneyPair;
typedef map< int, Money > MoneyMap;
MoneyMap map1;
map1.insert( MoneyPair( 1, mn[0] ) );
map1.insert( MoneyPair( 2, mn[1] ) );
map1.insert( MoneyPair( 3, mn[2] ) ); // (3)
map1.insert( MoneyPair( 4, mn[3] ) ); // (4)
MoneyMap map2;
map2.insert( MoneyPair( 3, mn[2] ) ); // (3)
map2.insert( MoneyPair( 4, mn[3] ) ); // (4)
map2.insert( MoneyPair( 5, mn[4] ) );
map2.insert( MoneyPair( 6, mn[5] ) );
map2.insert( MoneyPair( 7, mn[6] ) );
MoneyMap out;
MoneyMap::iterator out_itr( out.begin() );
set_intersection( map1.begin(), map1.end(), map2.begin(), map2.end(), inserter( out, out_itr ) );
cout << "intersection has " << out.size() << " elements." << endl;
return 0;
}
Since the pair labelled (3) and (4) appear in both maps, I was expecting that I'd get 2 elements in the intersection, but no, I get:
intersection has 0 elements.
I'm sure this is something to do with the comparitor on the map / pair but can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Niki 对于您的拼写错误肯定是正确的 -
map2
这里是空的!但是,您需要注意其他事情。假设您的代码如下所示:
现在会发生什么?您会发现
out
将为空,因为set_intersection
使用std::less
来比较元素,并且地图的元素是成对的 - - 因此 (3, mn[3]) 不同于 (3, mn[4])。另一种方法是编写
Now,
out
将包含两个元素:(3, mn[3]) 和 (4, mn[4]),因为它们的键< /em> 匹配。元素始终从第一个迭代器范围复制。请注意,地图始终按其包含的
map::value_compare
类型排序。如果您使用时髦的比较函数,如果映射的元素恰好不符合std:: 的顺序,则在没有显式提供比较函子的情况下,
set_intersection
将无法工作:更少。Niki is certainly correct about your typo --
map2
is empty here! However you need to be careful about something else.Let's say your code looked like this:
Now, what would happen? You'd find that
out
would be empty becauseset_intersection
usesstd::less
to compare elements, and the elements of your maps are pairs -- thus (3, mn[3]) differs from (3, mn[4]).The other way you could do this is by writing
Now,
out
will contain two elements: (3, mn[3]) and (4, mn[4]), because their keys match. The elements are always copied from the first iterator range.Note that maps are always sorted by the type
map::value_compare
they contain. If you're using a funky comparison function,set_intersection
will not work without the comparison functor explicitly supplied if the elements of the map don't happen to be in order with respect tostd::less
.除非这是一个拼写错误,否则您只是将内容重新插入到map1中,而不是插入到map2中。我用更正后的代码对其进行了测试,结果输出“Intersection has 2 elements”。
Unless this is a typo, you are just reinserting stuff into map1 instead of inserting into map2. I tested it out with the corrected code and it outputted "Intersection has 2 elements."