English 中文(简体)
如何收集同样的地图价值
原标题:How to collect the same type of Map s value

We have one map which key and value both are int type. We have to search a particular value in the map and collect those key in one vector. Code snapshot is like

map<int,int>m;
map<int,int>::iterator itr;
vector<int> v;
m.insert(make_pair<int,int>(1,2));
m.insert(make_pair<int,int>(2,2));
m.insert(make_pair<int,int>(3,2));
m.insert(make_pair<int,int>(4,4));
m.insert(make_pair<int,int>(5,5));

现行法典类似:

for ( itr = m.begin(); itr != m.end(); ++itr )
{
    if ((*itr).second == 2 )
    v.push_back((*itr).first )
}

我们愿优化这项工作。 我们如何能够使用STL算法。

问题回答

It seems to me that you are going about this the wrong way, you probably want a multimap.

std::multimap<int,int> m;
std::vector<int> v;
m.insert(std::make_pair<int,int>(2,1));
m.insert(std::make_pair<int,int>(2,2));
m.insert(std::make_pair<int,int>(2,3));
m.insert(std::make_pair<int,int>(4,4));
m.insert(std::make_pair<int,int>(5,5));

typedef std::multimap<int,int>::iterator iterator;
std::pair<iterator, iterator> bounds = m.equal_range(2);
for(iterator it = bounds.first; it != bounds.second; ++it)
   v.push_back(it->second);

您能否以价值交换钥匙(即所有价值不同)? 如果是,你可以使用地图(即O(log n))搜寻物品。 如果没有,你所写的守则就是正确的做法。

另一种做法是,在地图填满了价值时,制造病媒,但这假设过滤标准在插入时是已知的。

假设你确实做了许多工作,则有<代码>boost multi_index,尽管它只是作为斜体的平板,但可能不再努力维持两个地图,即:<条码>多图和图; int, int >, or <>code>map< int, set< int >>。

如果可以改变所有要求,即钥匙——价值可以转换,则可以采用<条码>地图和带;int、矢量和带;int>,并通过将数据推入与钥匙相应的病媒来绘制地图。 这样,就可以在绘制地图时优化地图。

如果无法改变要求,如同在一份评论中所说的那样,那么,我认为,优化的余地并不大。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签