使用多图 [英] Using a multimap

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

问题描述




有没有办法以这样的方式遍历多图?

只有唯一的密钥?换句话说,由于multimap允许重复的

键,我想遍历整个multimap,如果

特定键有重复,只能看到其中一个。我不认为我关心我看到哪些重复条目,因为一旦我有一个条目,我就可以使用lower_bound和upper_bound迭代它们, 对? (我

提前不知道密钥,所以我不能使用multimap :: find)


也许一个multimap甚至都不是解决问题的正确方法。这是我需要做的事情:我可能有一些项目(结构),其中一些项目可能在其中一个数据中包含相同的值成员。对于

实例:


struct MyStruct

{

int x;

...

};


可能有任意数量的MyStruct实例,而且有几个可能有相同的

''x''的值。如果是这种情况,它会识别出必须由用户更正的冲突。因此,我需要确定那些具有重复''x'的b / b $ b'的条目,以便用户知道冲突并且可以通过

来纠正它们。 (不幸的是,首先不可能防止冲突

- 这就是为什么存在这个问题的原因。)


我的想法是使用成员''''作为

键将条目复制到多图中,然后以某种方式识别多图中具有

重复键的条目。但是,我不确定如何做到这一点。

任何人都可以指出我正确的方向,或者为我的问题建议另一种解决方案吗?


也许另一种解决方案是使用地图和多图。我可以用相同的条目填写

。由于地图不允许重复,因此任何存在的重复数据都会过度。然后我可以遍历

地图(获取所有唯一键)并使用这些键作为multimap :: find中的搜索

条件。当然,如果条目列表很大,

这个解决方案可能会占用大量内存。


欢迎任何其他想法。


谢谢,


- 丹尼斯

解决方案

周一,16日2004年8月08:51:07 -0700,丹尼斯琼斯 < no **** @ bogus.com>

写道:



有没有办法迭代通过multimap以这种方式只能遇到唯一的键?换句话说,由于multimap允许重复的键,我想迭代整个multimap,如果
特定键有重复,只能看到其中一个。我不认为我关心我看到哪些重复的条目,因为一旦我有一个条目,我可以使用lower_bound和upper_bound来迭代它们,对吗? (我之前不知道密钥,所以我不能使用multimap :: find)

也许多图不是解决问题的正确方法。这是我需要做的事情:我可能有一些项目(结构),其中一些项目可能在其中一个数据成员中包含相同的值。对于
实例:

struct MyStruct
{
int x;
...
};

可能有任意数量的MyStruct实例,并且有几个可能具有相同的''x'值。如果是这种情况,它会识别必须由用户更正的冲突。因此,我需要识别那些具有重复''x'的条目,以便用户了解冲突并可以更正它们。 (不幸的是,首先不可能防止冲突 - 这就是为什么存在这个问题的原因。)

我的想法是将条目复制到多图中使用成员''''作为
键,然后以某种方式识别多图中具有重复键的条目。但是,我不确定如何做到这一点。
任何人都可以指出我正确的方向,或建议我的问题的替代解决方案吗?




std :: map< int,std :: vector< MyStruct> >

或者使用过滤迭代器跳过相同的键,使用不相等的键返回

下一个迭代器。


Tom


Dennis Jones写道:



有没有办法迭代通过multimap以这种方式只能遇到唯一的键?换句话说,由于multimap允许重复的键,我想迭代整个multimap,如果
特定键有重复,只能看到其中一个。我不认为我关心我看到哪些重复的条目,因为一旦我有一个条目,
我可以使用lower_bound和upper_bound来迭代它们,对吗? (我之前不知道密钥,所以我不能使用multimap :: find)

也许多图不是解决问题的正确方法。这是
我需要做什么:我可能有一些项目(结构),其中一些项目可能在其中一个数据中包含相同的值成员。对于
实例:

struct MyStruct
{
int x;
...
};

可能有任意数量的MyStruct实例,并且有些实例可能具有相同的x值。如果是这种情况,它会识别出必须由用户更正的冲突。因此,我需要识别那些具有重复''x'的条目,以便用户了解冲突并可以更正它们。 (不幸的是,首先不可能防止冲突 - 这就是为什么存在这个问题的原因。)

我的想法是将条目复制到多图中使用成员''''作为键,然后以某种方式识别多图中具有重复键的条目。但是,我不确定如何做到这一点。
任何人都可以指出我正确的方向,或建议我的问题的替代解决方案吗?

也许另一个解决方案是同时使用地图和多图。我可以用相同的条目填写
。由于地图不允许重复,因此存在的任何重复都会被夸大。然后我可以遍历地图(获取所有唯一键)并使用这些键作为multimap :: find中的
搜索条件。当然,如果条目列表很大,那么这个解决方案可能会占用大量内存。

欢迎任何其他想法。

谢谢,

- 丹尼斯




以下可能不是迭代通过

多图访问的最有效方式每个键的最低配对,但至少它是

易于阅读:


#include< map>

#包括< iostream>


int main(无效){

std :: multimap< int,int> t;

t.insert(std :: make_pair< int,int>(1,2));

t.insert(std :: make_pair< int,int> ;(1,3));

t.insert(std :: make_pair< int,int>(2,2));

t.insert(std: :make_pair< int,int>(1,6));

t.insert(std :: make_pair< int,int>(3,4));

t.insert(std :: make_pair< int,int>(3,1));

t.insert(std :: make_pair< int,int>(4,0));

for(std :: multimap< int,int> :: const_iterator iter = t.begin();

iter!= t.end();

iter = t.upper_bound(iter-> first)){

std :: cout<< iter-> first<< std :: endl;

}

}

最好


Kai-Uwe Bux


Dennis Jones写道:




有没有办法在这样的情况下迭代多图一种方式只能遇到唯一的钥匙?换句话说,由于multimap允许重复的键,我想迭代整个multimap,如果
特定键有重复,只能看到其中一个。我不认为我关心我看到哪些重复的条目,因为一旦我有一个条目,我可以使用lower_bound和upper_bound来迭代它们,对吗? (我之前不知道密钥,所以我不能使用multimap :: find)




为什么不简单地遍历多图和如果关键地图来了

up与上一次迭代循环中的键相同,你很明显

发现重复。


#include< iostream>

#include< map>

#include< string>


使用namespace std;


void main()

{

multimap< int,string> Mymap;


Mymap.insert(pair< int,string>(0," test1"));

Mymap.insert(pair< int, string>(2," test2"));

Mymap.insert(pair< int,string>(0," test3"));

Mymap。 insert(pair< int,string>(1," test4"));

Mymap.insert(pair< int,string>(2," test5"));

Mymap.insert(pair< int,string>(4," test6"));

Mymap.insert(pair< int,string>(3," test7") );


multimap< int,string> :: iterator it = Mymap.begin();

int LastKey = it-> first;

cout<<它 - >第一<< " " <<它 - >第二<< endl;

++ it;


while(it!= Mymap.end()){

cout<< ;它 - >第一<< " " << it-> second;

if(it-> first == LastKey)

cout<< "复制;

其他

LastKey = it-> first;

cout<< endl;

++ it;

}

}


-

Karl Heinz Buchegger
kb******@gascad.at


Hi,

Is there a way to iterate through a multimap in such a way as to encounter
only the unique keys? In other words, since a multimap allows duplicate
keys, I would like to iterate through the entire multimap, and if a
particular key has duplicates, only see one of them. I don''t think I care
about which of the duplicate entries I see, because once I have an entry, I
can use lower_bound and upper_bound to iterate through them, right? (I
won''t know the keys ahead of time, so I cannot use multimap::find)

Maybe a multimap isn''t even the right way to solve my problem. Here is what
I need to do: I may have some number of items (structures) in which some of
the items may contain the same value in one of its data members. For
instance:

struct MyStruct
{
int x;
...
};

There may be any number of MyStruct instances, and several may have the same
value for ''x''. If this is the case, it identifies a conflict which must be
corrected by the user. So, I need to identify those entries that have
duplicate ''x''s so that the user will be aware of the conflicts and can
correct them. (Unfortunately, it is not possible to prevent the conflicts
in the first place -- which is why this problem exists at all.)

My idea was to copy the entries into a multimap using the member ''x'' as the
key, and then somehow identify the entries in the multimap that have
duplicate keys. However, I am not sure how best to go about doing that.
Can anyone point me in the right direction, or suggest an alternative
solution to my problem?

Maybe another solution is to use both a map and a multimap. I could fill
both with the same entries. Since the map will not allow duplicates, any
duplicates that exist will get overritten. Then I could iterate through the
map (obtaining all of the unique keys) and use those keys as the search
criteria in multimap::find. Of course, if the list of entries is large,
this solution could use a lot of memory overhead.

Any other ideas are welcome.

Thanks,

- Dennis

解决方案

On Mon, 16 Aug 2004 08:51:07 -0700, "Dennis Jones" <no****@bogus.com>
wrote:

Hi,

Is there a way to iterate through a multimap in such a way as to encounter
only the unique keys? In other words, since a multimap allows duplicate
keys, I would like to iterate through the entire multimap, and if a
particular key has duplicates, only see one of them. I don''t think I care
about which of the duplicate entries I see, because once I have an entry, I
can use lower_bound and upper_bound to iterate through them, right? (I
won''t know the keys ahead of time, so I cannot use multimap::find)

Maybe a multimap isn''t even the right way to solve my problem. Here is what
I need to do: I may have some number of items (structures) in which some of
the items may contain the same value in one of its data members. For
instance:

struct MyStruct
{
int x;
...
};

There may be any number of MyStruct instances, and several may have the same
value for ''x''. If this is the case, it identifies a conflict which must be
corrected by the user. So, I need to identify those entries that have
duplicate ''x''s so that the user will be aware of the conflicts and can
correct them. (Unfortunately, it is not possible to prevent the conflicts
in the first place -- which is why this problem exists at all.)

My idea was to copy the entries into a multimap using the member ''x'' as the
key, and then somehow identify the entries in the multimap that have
duplicate keys. However, I am not sure how best to go about doing that.
Can anyone point me in the right direction, or suggest an alternative
solution to my problem?



std::map<int, std::vector<MyStruct> >
or use a filtering iterator that skips over equal keys, returning the
next iterator with a non-equal key.

Tom


Dennis Jones wrote:

Hi,

Is there a way to iterate through a multimap in such a way as to encounter
only the unique keys? In other words, since a multimap allows duplicate
keys, I would like to iterate through the entire multimap, and if a
particular key has duplicates, only see one of them. I don''t think I care
about which of the duplicate entries I see, because once I have an entry,
I
can use lower_bound and upper_bound to iterate through them, right? (I
won''t know the keys ahead of time, so I cannot use multimap::find)

Maybe a multimap isn''t even the right way to solve my problem. Here is
what
I need to do: I may have some number of items (structures) in which some
of
the items may contain the same value in one of its data members. For
instance:

struct MyStruct
{
int x;
...
};

There may be any number of MyStruct instances, and several may have the
same
value for ''x''. If this is the case, it identifies a conflict which must
be
corrected by the user. So, I need to identify those entries that have
duplicate ''x''s so that the user will be aware of the conflicts and can
correct them. (Unfortunately, it is not possible to prevent the conflicts
in the first place -- which is why this problem exists at all.)

My idea was to copy the entries into a multimap using the member ''x'' as
the key, and then somehow identify the entries in the multimap that have
duplicate keys. However, I am not sure how best to go about doing that.
Can anyone point me in the right direction, or suggest an alternative
solution to my problem?

Maybe another solution is to use both a map and a multimap. I could fill
both with the same entries. Since the map will not allow duplicates, any
duplicates that exist will get overritten. Then I could iterate through
the map (obtaining all of the unique keys) and use those keys as the
search
criteria in multimap::find. Of course, if the list of entries is large,
this solution could use a lot of memory overhead.

Any other ideas are welcome.

Thanks,

- Dennis



The following is probably not the most efficient way of iterating through a
multimap visiting only the lowest pair for each key, but at least it is
easy to read:

#include <map>
#include <iostream>

int main ( void ) {
std::multimap< int, int > t;
t.insert( std::make_pair<int,int>( 1,2 ) );
t.insert( std::make_pair<int,int>( 1,3 ) );
t.insert( std::make_pair<int,int>( 2,2 ) );
t.insert( std::make_pair<int,int>( 1,6 ) );
t.insert( std::make_pair<int,int>( 3,4 ) );
t.insert( std::make_pair<int,int>( 3,1 ) );
t.insert( std::make_pair<int,int>( 4,0 ) );
for( std::multimap< int, int >::const_iterator iter = t.begin();
iter != t.end();
iter = t.upper_bound( iter->first ) ) {
std::cout << iter->first << std::endl;
}
}
Best

Kai-Uwe Bux


Dennis Jones wrote:


Hi,

Is there a way to iterate through a multimap in such a way as to encounter
only the unique keys? In other words, since a multimap allows duplicate
keys, I would like to iterate through the entire multimap, and if a
particular key has duplicates, only see one of them. I don''t think I care
about which of the duplicate entries I see, because once I have an entry, I
can use lower_bound and upper_bound to iterate through them, right? (I
won''t know the keys ahead of time, so I cannot use multimap::find)



Why not simply iterate through the multimap and if the key the map comes
up with is identical to the key in the previous iteration loop, you obviously
found a duplicate.

#include <iostream>
#include <map>
#include <string>

using namespace std;

void main()
{
multimap< int, string > Mymap;

Mymap.insert( pair<int, string>( 0, "test1" ) );
Mymap.insert( pair<int, string>( 2, "test2" ) );
Mymap.insert( pair<int, string>( 0, "test3" ) );
Mymap.insert( pair<int, string>( 1, "test4" ) );
Mymap.insert( pair<int, string>( 2, "test5" ) );
Mymap.insert( pair<int, string>( 4, "test6" ) );
Mymap.insert( pair<int, string>( 3, "test7" ) );

multimap< int, string >::iterator it = Mymap.begin();
int LastKey = it->first;
cout << it->first << " " << it->second << endl;
++it;

while( it != Mymap.end() ) {
cout << it->first << " " << it->second;
if( it->first == LastKey )
cout << " duplicate";
else
LastKey = it->first;
cout << endl;
++it;
}
}

--
Karl Heinz Buchegger
kb******@gascad.at


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

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