找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 6449|回复: 4

stl标准算法find可以用在map上吗

[复制链接]
发表于 2009-8-13 11:23:17 | 显示全部楼层 |阅读模式
map有自己的find算法没错,但是现在有一个需求,要在map的某一些键的范围上查找second信息、
我用stl标准find,在map上编译不过去。

请问,标准算法可以用在map上吗,可否给几行例子?
发表于 2009-8-13 15:52:15 | 显示全部楼层
贴出错误信息。
发表于 2009-8-13 18:34:12 | 显示全部楼层
看参考资料,应该用类提供的find方法,效率更好。
Special Search Operations
Like sets and multisets, maps and multimaps provide special search member functions that perform better because of their internal tree structure (Table 6.28).

The find() member function searches for the first element that has the appropriate key and returns its iterator position. If no such element is found, find() returns end() of the container. You can't use the find() member function to search for an element that has a certain value. Instead, you have to use a general algorithm such as the find_if() algorithm, or program an explicit loop. Here is an example of a simple loop that does something with each element that has a certain value:

   std::multimap<std::string,float> coll;
   ...
   //do something with all elements having a certain value
   std::multimap<std::string,float>::iterator pos;
   for (pos = coll.begin(); pos != coll.end(); ++pos) {
      if (pos->second == value) {
          do_something();
      }
   }
Table 6.28. Special Search Operations of Maps and Multimaps Operation  Effect  
count(key)  Returns the number of elements with key key  
find(key)  Returns the position of the first element with key key or end()  
lower_bound(key)  Returns the first position where an element with key key would get inserted (the first element with key >= key)  
upper_bound(key)  Returns the last position where an element with key key would get inserted (the first element with key > key)  
equal_range(key)  Returns the first and last positions where elements with key key would get inserted (the range of elements with key == key)  

Be careful when you want to use such a loop to remove elements. It might happen that you saw off the branch on which you are sitting. See page 204 for details about this issue.

Using the find_if() algorithm to search for an element that has a certain value is even more complicated than writing a loop because you have to provide a function object that compares the value of an element with a certain value. See page 211 for an example.

The lower_bound(), upper_bound(), and equal_range() functions behave as they do for sets (see page 180), except that the elements are key/value pairs.
发表于 2010-2-9 22:40:31 | 显示全部楼层
typedef std::map< std::string, int > mydef;

struct _select_second
{
  int value;
  _select_second( int v ) : value(v) {}
  bool operator ()( mydef::value_type ele )
  {
     return ele.second == value;
  }
};

mydef vMap;
vMap["hello"] = 20;
mydef::iterator iter = std::find_if( vMap.begin(), vMap.end(), _select_second(20) );
发表于 2010-3-9 03:08:42 | 显示全部楼层
你说的find是algorithsm里面的find,不是map内部的find吧。
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-3 22:53 , Processed in 0.012050 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表