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:35http://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的遍曆。
查看原文