概述
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
怎么不给分?
===============================
for(iterator it = begin(); it != end(); ++it)
或者
for(iterator it = begin(); it != end(); it++)
如果这两种都对的话,区别是什么呢??
这个我知道,我想知道这两种遍历的结果是否一样?为什么网上这两种遍历都有出现?
对于两种方式来说:
for(iterator it = begin(); it != end(); ++it)
{
return it->second;
}
for(iterator it = begin(); it != end(); it++)
{
return it->second;
}
每一次返回的结果是否相同??
不信的话你可以去看看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.
处理map遍历的binder类: depair
最后
以上就是超级烤鸡为你收集整理的stl 中怎样遍历一个map中的所有元素。请给是实例处理map遍历的binder类: depair的全部内容,希望文章能够帮你解决stl 中怎样遍历一个map中的所有元素。请给是实例处理map遍历的binder类: depair所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复