鍑芥暟瀵硅薄涓庡嚱鏁版寚閽堢浉姣旇緝鏈変袱涓柟闈㈢殑浼樼偣錛氶鍏堝鏋滆閲嶈澆鐨勮皟鐢ㄦ搷浣滅鏄痠nline鍑芥暟鍒欑紪璇戝櫒鑳藉鎵ц鍐呰仈緙栬瘧錛屾彁渚涘彲鑳界殑鎬ц兘濂藉錛涘叾嬈″嚱鏁板璞″彲浠ユ嫢鏈変換鎰忔暟鐩殑棰濆鏁版嵁錛岀敤榪欎簺鏁版嵁鍙互緙撳啿緇撴灉錛屼篃鍙互緙撳啿鏈夊姪浜庡綋鍓嶆搷浣滅殑鏁版嵁銆?/P>
鍑芥暟瀵硅薄鏄竴涓被錛屽畠閲嶈澆浜?jiǎn)鍑芥暟璋冪敤鎿嶄綔绗perator() ,璇ユ搷浣滅灝佽浜?jiǎn)涓涓嚱鏁扮殑鍔熻兘銆傚吀鍨嬫儏鍐典笅鍑芥暟瀵硅薄琚綔涓哄疄鍙備紶閫掔粰娉涘瀷綆楁硶錛屽綋鐒舵垜浠篃鍙互瀹氫箟鐙珛鐨勫嚱鏁板璞″疄渚嬨?/P>
鏉ョ湅涓嬮潰鐨勪簩涓緥瀛愶細(xì) 姣旇緝鐞嗚В浼?xì)鏇村ソ浜涘Q?/P>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
class Sum {
int val;
public:
Sum(int i) :val(i) { }
//褰撳湪闇瑕乮nt鐨勫湴鏂癸紝Sum灝嗚嚜鍔ㄨ漿鎹負(fù)int綾誨瀷
//榪欓噷鏄負(fù)浜?jiǎn)鏂逛究cout<<Sum鐨勫疄渚嬶紱
operator int() const { return val; }
//鍐欏湪綾諱腑鐨勫嚱鏁頒唬鐮佷竴鑸粯璁や負(fù)鍐呰仈浠g爜
int operator()(int i) { return val+=i; }
};
void f(vector<int> v)
{
Sum s = 0; //Sum s = 0絳変環(huán)浜嶴um s(0),涓嶇瓑浠蜂簬Sum s;s = 0;
//瀵箆ector<int>涓殑鍏冪礌姹傚拰
//鍑芥暟瀵硅薄琚綔涓哄疄鍙備紶閫掔粰娉涘瀷綆楁硶
s = for_each(v.begin(), v.end(), s);
cout << "the sum is " << s << "\n";
//鏇寸畝鍗曠殑鍐欐硶錛屽畾涔夌嫭绔嬬殑鍑芥暟瀵硅薄瀹炰緥
cout << "the sum is " << for_each(v.begin(), v.end(), Sum(0)) << "\n";
}
int main()
{
vector<int> v;
v.push_back(3); v.push_back(2); v.push_back(1);
f(v);
system("pause");
return 0;
}
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?BR>#include <iostream>
#include <list>
#include <algorithm>
#include "print.hpp"
using namespace std;
// function object that adds the value with which it is initialized
class AddValue {
private:
int theValue; // the value to add
public:
// constructor initializes the value to add
AddValue(int v) : theValue(v) {
}
// the ``function call'' for the element adds the value
void operator() (int& elem) const {
elem += theValue;
}
};
int main()
{
list<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: ");
// add value 10 to each element
for_each (coll.begin(), coll.end(), // range
AddValue(10)); // operation
PRINT_ELEMENTS(coll,"after adding 10: ");
// add value of first element to each element
for_each (coll.begin(), coll.end(), // range
AddValue(*coll.begin())); // operation
PRINT_ELEMENTS(coll,"after adding first element: ");
}
-------------------------------------------------------------------------
operator()涓殑鍙傛暟涓篶ontainer涓殑鍏冪礌
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?BR>
鍙﹀鐨勫疄渚嬶細(xì)
Function Objects as Sorting Criteria
Programmers often need a sorted collection of elements that have a special class (for example, a collection of persons). However, you either don't want to use or you can't use the usual operator < to sort the objects. Instead, you sort the objects according to a special sorting criterion based on some member function. In this regard, a function object can help. Consider the following example:
// fo/sortl.cpp #include <iostream> #include <string> #include <set> #include <algorithm> using namespace std; class Person { public: string firstname() const; string lastname() const; ... }; /* class for function predicate * - operator() returns whether a person is less than another person */ class PersonSortCriterion { public: bool operator() (const Person& p1, const Person& p2) const { /* a person is less than another person * - if the last name is less * - if the last name is equal and the first name is less */ return p1.lastname()<p2.1astname() || (! (p2.1astname()<p1.lastname()) && p1.firstname()<p2.firstname()); } }; int main() { //declare set type with special sorting criterion typedef set<Person,PersonSortCriterion> PersonSet; //create such a collection PersonSet coll; ... //do something with the elements PersonSet::iterator pos; for (pos = coll.begin(); pos != coll.end();++pos) { ... } ... }
//fo/foreach3.cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; //function object to process the mean value class MeanValue { private: long num; //number of elements long sum; //sum of all element values public: //constructor MeanValue() : num(0), sum(0) { } //"function call" //-process one more element of the sequence void operator() (int elem) { num++; //increment count sum += elem; //add value } //return mean value double value() { return static_cast<double>(sum) / static_cast<double>(num); } }; int main() { vector<int> coll; //insert elments from 1 to 8 for (int i=1; i<=8; ++i) { coll.push_back(i); } //process and print mean value MeanValue mv = for_each (coll.begin(), coll.end(), //range MeanValue()); //operation cout << "mean value: " << mv.value() << endl; }
(T) expression // cast expression to be of type T
鍑芥暟椋庢牸錛團(tuán)unction-style錛夊己鍒惰漿鍨嬩嬌鐢ㄨ繖鏍風(fēng)殑璇硶錛?BR> T(expression) // cast expression to be of type T
榪欎袱縐嶅艦寮忎箣闂存病鏈夋湰璐ㄤ笂鐨勪笉鍚岋紝瀹冪函綺瑰氨鏄竴涓妸鎷彿鏀懼湪鍝殑闂銆傛垜鎶婅繖涓ょ褰㈠紡縐頒負(fù)鏃ч鏍鹼紙old-style錛夌殑寮哄埗杞瀷銆?
浜屻?C++鐨勫洓縐嶅己鍒惰漿鍨嬪艦寮忥細(xì)
銆銆C++ 鍚屾椂鎻愪緵浜?jiǎn)鍥浛U嶆柊鐨勫己鍒惰漿鍨嬪艦寮忥紙閫氬父縐頒負(fù)鏂伴鏍肩殑鎴?C++ 椋庢牸鐨勫己鍒惰漿鍨嬶級(jí)錛?
銆銆const_cast(expression)
銆銆dynamic_cast(expression)
銆銆reinterpret_cast(expression)
銆銆static_cast(expression)
銆銆姣忎竴縐嶉傜敤浜庣壒瀹氱殑鐩殑錛?
銆銆dynamic_cast 涓昏鐢ㄤ簬鎵ц鈥滃畨鍏ㄧ殑鍚戜笅杞瀷錛坰afe downcasting錛夆濓紝涔熷氨鏄錛岃紜畾涓涓璞℃槸鍚︽槸涓涓戶鎵夸綋緋諱腑鐨勪竴涓壒瀹氱被鍨嬨傚畠鏄敮涓涓嶈兘鐢ㄦ棫椋庢牸璇硶鎵ц鐨勫己鍒惰漿鍨嬶紝涔熸槸鍞竴鍙兘鏈夐噸澶ц繍琛屾椂浠d環(huán)鐨勫己鍒惰漿鍨嬨?BR>
static_cast 鍙互琚敤浜庡己鍒墮殣鍨嬭漿鎹紙渚嬪錛宯on-const 瀵硅薄杞瀷涓?const 瀵硅薄錛宨nt 杞瀷涓?double錛岀瓑絳夛級(jí)錛屽畠榪樺彲浠ョ敤浜庡緢澶氳繖鏍風(fēng)殑杞崲鐨勫弽鍚戣漿鎹紙渚嬪錛寁oid* 鎸囬拡杞瀷涓烘湁綾誨瀷鎸囬拡錛屽熀綾繪寚閽堣漿鍨嬩負(fù)媧劇敓綾繪寚閽堬級(jí)錛屼絾鏄畠涓嶈兘灝嗕竴涓?const 瀵硅薄杞瀷涓?non-const 瀵硅薄錛堝彧鏈?const_cast 鑳藉仛鍒幫級(jí)錛屽畠鏈鎺ヨ繎浜嶤-style鐨勮漿鎹€?BR>
銆銆 const_cast 涓鑸敤浜庡己鍒舵秷闄ゅ璞$殑甯擱噺鎬с傚畠鏄敮涓鑳藉仛鍒拌繖涓鐐圭殑 C++ 椋庢牸鐨勫己鍒惰漿鍨嬨?
銆銆 reinterpret_cast 鏄壒鎰忕敤浜庡簳灞傜殑寮哄埗杞瀷錛屽鑷村疄鐜頒緷璧栵紙implementation-dependent錛夛紙灝辨槸璇達(dá)紝涓嶅彲縐繪錛夌殑緇撴灉錛屼緥濡傦紝灝嗕竴涓寚閽堣漿鍨嬩負(fù)涓涓暣鏁般傝繖鏍風(fēng)殑寮哄埗杞瀷鍦ㄥ簳灞備唬鐮佷互澶栧簲璇ユ瀬涓虹綍瑙併?BR>銆銆
銆銆鏃ч鏍肩殑寮哄埗杞瀷渚濈劧鍚堟硶錛屼絾鏄柊鐨勫艦寮忔洿鍙彇銆傞鍏堬紝鍦ㄤ唬鐮佷腑瀹冧滑鏇村鏄撹瘑鍒紙鏃犺鏄漢榪樻槸鍍?grep 榪欐牱鐨勫伐鍏烽兘鏄姝わ級(jí)錛岃繖鏍峰氨綆鍖栦簡(jiǎn)鍦ㄤ唬鐮佷腑瀵繪壘綾誨瀷緋葷粺琚牬鍧忕殑鍦版柟鐨勮繃紼嬨傜浜岋紝鏇寸簿紜湴鎸囧畾姣忎竴涓己鍒惰漿鍨嬬殑鐩殑錛屼嬌寰楃紪璇戝櫒璇婃柇浣跨敤閿欒鎴愪負(fù)鍙兘銆備緥濡傦紝濡傛灉浣犺瘯鍥句嬌鐢ㄤ竴涓?const_cast 浠ュ鐨勬柊椋庢牸寮哄埗杞瀷鏉ユ秷闄ゅ父閲忔э紝浣犵殑浠g爜灝嗘棤娉曠紪璇戙?
==
== dynamic_cast .vs. static_cast
==
class B { ... };
class D : public B { ... };
void f(B* pb)
{
D* pd1 = dynamic_cast<D*>(pb);
D* pd2 = static_cast<D*>(pb);
}
If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.
If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer鈥檚 assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.
鍗砫ynamic_cast鍙敤浜庣戶鎵夸綋緋諱腑鐨勫悜涓嬭漿鍨嬶紝鍗沖皢鍩虹被鎸囬拡杞崲涓烘淳鐢熺被鎸囬拡錛屾瘮static_cast鏇翠弗鏍兼洿瀹夊叏銆俤ynamic_cast鍦ㄦ墽琛屾晥鐜囦笂姣攕tatic_cast瑕佸樊涓浜?浣唖tatic_cast鍦ㄦ洿瀹戒笂鑼冨洿鍐呭彲浠ュ畬鎴愭槧灝?榪欑涓嶅姞闄愬埗鐨勬槧灝勪即闅忕潃涓嶅畨鍏ㄦ?static_cast瑕嗙洊鐨勫彉鎹㈢被鍨嬮櫎綾誨眰嬈$殑闈?rùn)鎬佸鑸互澶?榪樺寘鎷棤鏄犲皠鍙樻崲,紿勫寲鍙樻崲(榪欑鍙樻崲浼?xì)瀵艰嚧瀵硅薄鍒囩?涓㈠け淇℃伅),鐢╒OID*鐨勫己鍒跺彉鎹?闅愬紡綾誨瀷鍙樻崲絳?..
==
== static_cast .vs. reinterpret_cast
==
reinterpret_cast鏄負(fù)浜?jiǎn)鏄牉畡鍒颁竴涓畬鍏ㄤ笉鍚岀被鍨嬬殑鎰忔?榪欎釜鍏抽敭璇嶅湪鎴戜滑闇瑕佹妸綾誨瀷鏄犲皠鍥炲師鏈夌被鍨嬫椂鐢ㄥ埌瀹?鎴戜滑鏄犲皠鍒扮殑綾誨瀷浠呬粎鏄負(fù)浜?jiǎn)鏁呭紕鐜勮櫄鍜屽叾浠栫洰鐨?榪欐槸鎵鏈夋槧灝勪腑鏈鍗遍櫓鐨?(榪欏彞璇濇槸C++緙栫▼鎬濇兂涓殑鍘熻瘽)
static_cast 鍜?reinterpret_cast 鎿嶄綔絎︿慨鏀逛簡(jiǎn)鎿嶄綔鏁扮被鍨? 瀹冧滑涓嶆槸浜掗嗙殑; static_cast 鍦ㄧ紪璇戞椂浣跨敤綾誨瀷淇℃伅鎵ц杞崲, 鍦ㄨ漿鎹㈡墽琛屽繀瑕佺殑媯(gè)嫻?璇稿鎸囬拡瓚婄晫璁$畻, 綾誨瀷媯(gè)鏌?. 鍏舵搷浣滄暟鐩稿鏄畨鍏ㄧ殑. 鍙︿竴鏂歸潰, reinterpret_cast 浠呬粎鏄噸鏂拌В閲婁簡(jiǎn)緇欏嚭鐨勫璞$殑姣旂壒妯″瀷鑰屾病鏈夎繘琛屼簩榪涘埗杞崲, 渚嬪瓙濡備笅:
int n=9; double d=static_cast < double > (n);
涓婇潰鐨勪緥瀛愪腑, 鎴戜滑灝嗕竴涓彉閲忎粠 int 杞崲鍒?double. 榪欎簺綾誨瀷鐨勪簩榪涘埗琛ㄨ揪寮忔槸涓嶅悓鐨? 瑕佸皢鏁存暟 9 杞崲鍒?鍙岀簿搴︽暣鏁?9, static_cast 闇瑕佹紜湴涓哄弻綺懼害鏁存暟 d 琛ヨ凍姣旂壒浣? 鍏剁粨鏋滀負(fù) 9.0. 鑰宺einterpret_cast 鐨勮涓哄嵈涓嶅悓:
int n=9;
double d=reinterpret_cast<double & > (n);
榪欐, 緇撴灉鏈夋墍涓嶅悓. 鍦ㄨ繘琛岃綆椾互鍚? d 鍖呭惈鏃犵敤鍊? 榪欐槸鍥犱負(fù) reinterpret_cast 浠呬粎鏄鍒?n 鐨勬瘮鐗逛綅鍒?d, 娌℃湁榪涜蹇呰鐨勫垎鏋?