stl 中怎样遍历一个map中的所有元素。请给是实例

http://topic.csdn.net/t/20020309/18/565440.html
  stl 中怎样遍历一个map中的所有元素。请给是实例
楼主vlmf(散淡书生) 2002-03-09 18:09:15 在 C/C++ / C 语言 提问

stl   中怎样遍历一个map中的所有元素。请给是实例 问题点数:50、回复次数:5Top


1 楼hhdsq(流氓宝宝)回复于 2002-03-09 18:19:32 得分 40

从essential   c++上搬来的,还没验证:  
   
  #include<map>  
  #include<string>  
  #include<iostream>  
   
  int   main()  
  {  
  map<string,int>   words;  
  map<string,int>::iterator   it=words.begin();  
  for(;it!=words.end();++it)  
            cout<<"key:"<<it->first  
                    <<"value:"<<it->second<<end1;  
  return   0;  
  }Top

2 楼hhdsq(流氓宝宝)回复于 2002-03-09 18:21:07 得分 0

当然,words这个map里面首先得有元素。。Top

3 楼fangrk(加把油,伙计!)回复于 2002-03-09 20:36:46 得分 0

http://www.csdn.net/expert/topic/552/552836.xml?temp=.4185602  
  “我想写一个参数为字符串,返回类型为char的一个函数,其作用是返回字符串中出现频率最多的一个字符,请高手给出实现代码,非常非常非常感谢!!!!”  
     
      //bcc     5    
  #include     <map>    
  #include     <iostream>    
  using     namespace     std;    
  char     maxCount(const     char     *);    
   
  void     main()    
  {                 char     buff[200];    
                      cout<<"Please     input     string:"<<endl;    
                      cin>>buff;    
                      cout<<"The     max     count     char     in     buff     is:"<<maxCount(buff)<<endl;    
  }    
  char     maxCount(const     char     *     string)    
  {             map<char,int>     c_map;    
                  const     char     *p=string;    
                  while(*p){    
                  c_map[*p]++;    
                  p++;    
                  }    
                  int     max=0;    
                  map<char,int>::iterator     it=c_map.begin();    
                  char   find=it->first;  
                  for(;it!=c_map.end();it++){    
                  if(max<it->second){    
                              max=it->second;    
                              find=it->first;    
                  }    
                  }    
                  return     find;    
  }    
  Top

4 楼zheng_can(nothrow)回复于 2002-03-09 20:48:28 得分 10

用   map<>::iteratorTop

5 楼fangrk(加把油,伙计!)回复于 2002-03-11 15:42:03 得分 0

怎么不给分?


===============================

问一个stl的map遍历的问题
悬赏分:20 - 解决时间:2007-8-29 09:57
看了很多遍历map的文章,里面的代码有很多,但是也让我很疑惑。想知道遍历到底是下面两种的哪一种?
for(iterator it = begin(); it != end(); ++it)

或者
for(iterator it = begin(); it != end(); it++)

如果这两种都对的话,区别是什么呢??
问题补充:aruo0228:

这个我知道,我想知道这两种遍历的结果是否一样?为什么网上这两种遍历都有出现?

对于两种方式来说:
for(iterator it = begin(); it != end(); ++it)
{
return it->second;
}

for(iterator it = begin(); it != end(); it++)
{
return it->second;
}
每一次返回的结果是否相同??
提问者: lemonbox - 试用期 一级
最佳答案
两 种方式iterator遍历的次数是相同的,但在STL中效率不同,前++--返回引用,后++--返回一个临时对象,因为iterator是类模板,使 用it++这种形式要返回一个无用的临时对象,而it++是函数重载,所以编译器无法对其进行优化,所以每遍历一个元素,你就创建并销毁了一个无用的临时 对象。

不信的话你可以去看看C++的标准库,还有符合标准C++的教材,除了特殊需要和对内置类型外,基本都是使用++it来进行元素遍历的,不管是源代码还是教材中都是如此。

用户定义类型对操作符的重载应与内置操作符的行为相似,而且后自增/减往往是引用前自增/减来作为其实行的一个副本。

比如通常都是这种形式:

class foo
{
public:
foo& operator ++ (){return ++bar;}

foo operator ++ (int)
{
foo tmp = *this; // 创建临时对象 ★
++*this; // 调用前自增
return tmp; // 返回临时对象 ★
}

private:
int bar;
}

以上标★号的2个步骤有时是多余的,比如用STL中用iterator遍历容器,这样就造成了不必要的程序效率的损失。

这也是被一些从C移植到C++的程序员所频频忽视的细节,所以它们被称为从C带到C++中的编程恶习。

More Effective C++
Item 6: Distinguish between prefix and postfix forms of increment and decrement operators.

对C++中的前/后自增/减操作符以及因C++的重载对他们所引发的效率问题有详细的讲解。以下是一部分内容:

If you're the kind who worries about efficiency, you probably broke into a sweat when you first saw the postfix increment function. That function has to create a temporary object for its return value (see Item 19), and the implementation above also creates an explicit temporary object (oldValue) that has to be constructed and destructed. The prefix increment function has no such temporaries. This leads to the possibly startling conclusion that, for efficiency reasons alone, clients of UPInt should prefer prefix increment to postfix increment unless they really need the behavior of postfix increment. Let us be explicit about this.

When dealing with user-defined types, prefix increment should be used whenever possible, because it's inherently more efficient. (注意这一句)

Let us make one more observation about the prefix and postfix increment operators. Except for their return values, they do the same thing: they increment a value. That is, they're supposed to do the same thing. How can you be sure the behavior of postfix increment is consistent with that of prefix increment? What guarantee do you have that their implementations won't diverge over time, possibly as a result of different programmers maintaining and enhancing them? Unless you've followed the design principle embodied by the code above, you have no such guarantee. That principle is that postfix increment and decrement should be implemented in terms of their prefix counterparts. You then need only maintain the prefix versions, because the postfix versions will automatically behave in a consistent fashion.
回答者: 飘渺世间天 - 参将 九级   8-28 20:35
http://zhidao.baidu.com/question/34261203.html



您的位置: 首页 > 技术频道 > 正文

处理map遍历的binder类: depair

[ 收藏此页] [ 打印]

【IT168知识库】
 

因为STL库广泛使用了Iterator, functor, binder,因此可以写出一个能够融入STL库的Adapter类,提高代码的通用性。
比 如STL算法中传入的functor型别要求为ReturnType& func(Type& elem),而map中同时存了key和value,有时候需要使用ReturnType& func(KeyType& key, ValueType& value)的回调,那就可以写这样一个functor
 
template<typename Operation>
class depair
    : public unary_function<
                 pair<
                      typename Operation::first_argument_type, 
                      typename Operation::second_argument_type>,
                 typename Operatoin::result_type>
{
    public:
        typedef pair<
                     typename Operation::first_argument_type, 
                     typename Operation::second_argument_type> argument_type;
        typedef typename Operation::result_type result_type;
        
        depair(const Operation& func) : op(func) { };
        
        result_type operator() (const argument_type& arg)
        {
            return op(arg.first, arg.second);           
        }
        
        result_type operator() (const argument_type& arg) const
        {
            return op(arg.first, arg.second);
        }
    protected:
        Operation op;        
};
 
有了depair后,就可以使用以下的代码
 
void traverse(const int& key, const string& value)
{
 
}
 
map<int, string> mymap;
 
std::for_each(mymap.begin(), mymap.end(), depair<std::ptr_fun(traverse)>());
 
并且depair可以和其他的binder一起使用,比如
void check(const int& key, const string& value, int check_value)
{
 
}
 
map<int, string> mymap;
 
std::for_each(mymap.begin(), mymap.end(), depair<boost::bind(check, _3, 100)>());
 
上面的一行代码中,首先用boost::bind将check的第三个参数绑定值100,也就是传给check方法的check_value值固定为100,然后再绑定到depair去处理map的遍历。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值