#眉標=VS2008、Visual C++、MFC #副標=體驗新一代整合開發環境(18) #大標=Visual C++ 2008強化UI介面風格 #作者=文/圖 王寧疆 ===<反灰>============= #include #include using namespace std::tr1; void main() { shared_ptr sp(new int(123)); //動態配置存放123數值的記憶體,並交給名稱為sp的智慧指標管理 assert(0 != sp); //確認動態配置記憶體的動作有成功 shared_ptr sp2(sp); //將sp智慧指標複製給sp2智慧指標 } ================ ===<反灰>============= #include #include #include using namespace std::tr1; void main() { shared_ptr sp(new int); //命令sp智慧指標指向動態配置的int型態的物件 cout << sp.use_count() << endl; //查詢sp智慧指標管理的記憶體的參考次數 weak_ptr wp(sp); //將sp智慧指標複製給weak_ptr智慧指標wp cout << sp.use_count() << endl; //確認sp智慧指標參考的記憶體的參考次數等於1 } ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; void main() {   cmatch res; //建立處理符合比對結果的物件   string str = "

Egg prices

"; //被比對的字串   regex rx("([^<]+)"); //指定執行比對的格式   regex_search(str.c_str(), res, rx); //執行比對,並將比對結果交給res物件處理   cout << res[1] << ". " << res[2] << endl; //印出標籤設定的文字大小和標籤的內容 } ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; void main() {   string str = "Hello world"; //被置換的文字   regex rx("world"); //欲置換的內容   string replacement = "planet"; //置換後的新內容   string str2 = regex_replace(str, rx, replacement); //呼叫regex_replace方法執行置換   cout << "原始文字:" << str << endl; //印出原始文字   cout << "置換後文字:" << str2 << endl; //印出置換後的文字 } ================ ===<反灰>============= string str2 = regex_replace(str, rx, replacement, regex_constants::format_first_only); ================ ===<反灰>============= regex rx("world", regex_constants::icase); ================ ===<反灰>============= #include #include #include #include #include using namespace std; using namespace std::tr1; template void RemoveDuplicate(Func func) //負責移除陣列中內容值重覆的元素的樣版函數 { array data = { 1, 1, 2, 3, 3, 3, 5 }; //宣告內含7個整數的陣列 array::iterator end = unique(data.begin(), data.end(), func); //找出陣列中不重覆的元素 copy(data.begin(), end, ostream_iterator(cout, " ")); //將不重覆的元素複製到data陣列 cout << endl; //輸出換行符號 } void main() { RemoveDuplicate(equal_to()); //利用equal_to函數物件移除陣列中重覆的元素 } ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; void f(int &r) //參數為int&的函數 { ++r; //遞增參數的內容值 } template void g(Funct f, Arg t) //參數為函數物件的樣版函數 { f(t); //利用函數物件呼叫函數 } int main() { int i = 0; g(f, i); //使用傳統的參考語法傳遞參數 cout << i << endl; //傳遞參數的位址失敗,印出0 g(f, ref(i)); //使用物件參考封裝傳遞參數 cout << i << endl; //傳遞參數的位址成功,印出1 } ================ ===<反灰>============= #include #include #include using namespace std; using namespace std::tr1; int main() { minstd_rand gen; //建立linear_congruential型態的亂數產生器 gen.seed((unsigned int)time(NULL)); //以目前時間當做亂數種子 for(int i = 0; i < 10; ++i) //利用迴圈產生10個亂數 { cout << gen() << " "; //印出亂數內容 } cout << endl; //換行 } ================ ===<反灰>============= #include #include #include using namespace std; using namespace std::tr1; int main() { minstd_rand gen; //建立linear_congruential亂數產生器 uniform_int dist(0, 9); //建立介於0~9之間的平均分佈 gen.seed((unsigned int)time(NULL)); //以目前時間當做亂數種子 for(int i = 0; i < 10; ++i) //利用迴圈產生10個亂數 { cout << dist(gen) << " "; //印出亂數內容 } cout << endl; //換行 } ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; int Add(int x, int y) //名稱為Add的函數 {  return x + y; } class Adder //Adder類別 { public: int Add(int x, int y) //Add方法  { return x + y; } }; typedef int (*FunctionPointerType)(int x, int y); //宣告多型函數封裝,可以代表接受兩個int型態參數的函數 int main() {  FunctionPointerType fp1 = &Add; //宣告代表Add函數的多型函數封裝fp1 int result = fp1(4, 5); //呼叫fp1所代表的函數 cout << result << endl; //印出呼叫結果 Adder adder; //建立Adder類別的物件 typedef int (Adder::*MemberFunctionPointerType)(int x, int y); //定義多型函數封裝型態 MemberFunctionPointerType mfp=&Adder::Add; //宣告代表Adder類別的Add方法的多型函數封裝mfp result = (adder.*mfp)(4, 5); //呼叫mfp所代表的函數 cout << result << endl; //印出呼叫結果 } ================ ===<反灰>============= #include #include #include using namespace std; using namespace std::tr1; typedef tuple tuple_ids; //定義tuple_ids為tuple型態 void print_tuple(tuple_ids& t) //印出tuple內容的函數 { cout << get<0>(t) << " " //印出元素的第一項資料 << get<1>(t) << " " //印出元素的第二項資料 << get<2>(t) << endl; //印出元素的第三項資料 } int main() { tuple_ids t(1, 2.5, "three"); //存放內含1,2.5,和"there"三項資料的元素到tuple中 print_tuple(t); //呼叫print_tuple函數,印出tuple的內容 } ================ ===<反灰>============= get<0>(t) = 10; //填入10到tuple元素的第一項資料 get<1>(t) = 20.5; //填入20.5到tuple元素的第二項資料 get<2>(t) = "thirty"; //填入"thirty"到tuple元素的第三項資料 ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; int main() { array arr = {1, 2, 3}; //宣告可以存放int型態元素的array集合 for(array::const_iterator it = arr.begin();it != arr.end();++it) //利用iterator讀取集合的元素 { cout << *it << " "; //列印元素內容 } cout << endl; } ================ ===<反灰>============= array arr; //宣告元素型態為int的array集合 arr.assign(1); //將集合中所有的元素的內容值都設定為1 ================ ===<反灰>============= array arr = {1, 2, 3}; //宣告元素型態為int的array集合 for(array::const_reverse_iterator rit =arr.rbegin();rit != arr.rend();++rit) //反向取用集合元素的內容 { cout << *rit << " "; //印出所取得的元素內容 } ================ ===<反灰>============= array arr = {1, 2, 3}; //宣告元素型態為int的array集合 for(size_t i = 0; i < 3; ++i) //利用迴圈取用集合的內容 { try { cout << arr.at(i) << " "; //印出集合第i個元素的內容 } catch(out_of_range& ex) //處理out_of_range型態的例外 { cout << ex.what() << endl; //印出例外的內容 } } ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1 //定義Mymap為可以存放由字元和整數組成的元素的unordered_map集合 typedef unordered_map Mymap; void main() { Mymap c1; //宣告c1為Mymap型態的集合 c1.insert(Mymap::value_type('a', 1)); //放入由'a', 1組成的元素到集合中 c1.insert(Mymap::value_type('b', 2)); //放入由'b', 2組成的元素到集合中 c1.insert(Mymap::value_type('c', 3)); //放入由'c', 3組成的元素到集合中 for (Mymap::iterator it = c1.begin();it!=c1.end();++it)//利用迴圈搭配iterator取出集合中所有的元素 cout << " [" << it->first << ", " << it->second << "]"; //印出集合的內容 cout << endl; //換行 } ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; struct trivial //宣告名稱為trivial的非抽象資料型態 { int val; }; struct abstract //宣告名稱為abstract的抽象資料型態 { virtual int val() = 0; }; void main() { cout << "is_abstract == " << boolalpha << is_abstract::value << endl; //利用is_abstract判斷trivial是否為抽象資料型態 cout << "is_abstract == " << boolalpha << is_abstract::value << endl; //利用is_abstract判斷abstract是否為抽象資料型態 } ================ ===<反灰>============= is_abstract == false is_abstract == true ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; struct base //宣告名稱為base的資料型態 { int val; }; struct derived : public base //宣告名稱為derived的資料型態繼承自base型態 { }; void main() { //利用is_base_of類別判斷base型態是否繼承自base型態 cout << "is_base_of == " << boolalpha << is_base_of::value << endl; //利用is_base_of類別判斷derived型態是否繼承自base型態 cout << "is_base_of == " << boolalpha << is_base_of::value << endl; //利用is_base_of類別判斷base型態是否繼承自derived型態 cout << "is_base_of == " << boolalpha << is_base_of::value << endl; } ================ ===<反灰>============= is_base_of == true is_base_of == true is_base_of == false ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; class Funs //定義Funs類別 { public: void square(double x) //定義square方法 { cout << x << "^2 = " << x * x << endl; } void product(double x, double y) //定義product方法 { cout << x << "*" << y << " = " << x * y << endl; } }; void main() { Funs funs; //建立Funs類別的物件 mem_fn(&Funs::square)(funs, 3.0); //封裝並呼叫square mem_fn(&Funs::product)(funs, 3.0, 2.0); //封裝並呼叫product } ================ ===<反灰>============= 3^2=9 3*2=6 ================ ===<反灰>============= #include #include using namespace std; using namespace std::tr1; typedef unordered_map Mymap; //定義Mymap型態為可以存放由字元和整數組成的unordered_map集合 void main() { Mymap c1; //宣告Mymap型態的變數 Mymap::hasher hfn = c1.hash_function(); //利用unordered_map集合的hash_function方法取得雜湊函數 cout << "hfn('a') == " << hfn('a') << endl; //使用取得的雜湊函數對字元a進行雜湊運算 cout << "hfn('b') == " << hfn('b') << endl; //使用取得的雜湊函數對字元b進行雜湊運算 } ================ ===<反灰>============= hfn('a') == 1630279 hfn('b') == 1647086 ================ ===<反灰>============= #include #include #include using namespace std; using namespace std::tr1; using namespace std::tr1::placeholders; void square(double x) //定義square方法 { cout << x << "^2 = " << x * x << endl; } void product(double x, double y) //定義product方法 { cout << x << "*" << y << " = " << x * y << endl; } void main() { double arg[] = {1, 2, 3}; //宣告元素型態為double的陣列 for_each(&arg[0],&arg[3] ,square); //利用for_each迴圈逐一取出arg陣列的元素再傳給square函數當做參數 cout << endl; //換行 for_each(&arg[0], &arg[3], bind(product, _1, 2)); //利用for_each迴圈逐一取出arg陣列的元素再傳給product函數當做參數 cout << endl; //換行 for_each(&arg[0], &arg[3], bind(square, _1)); //利用for_each迴圈逐一取出arg陣列的元素再傳給square函數當做參數 } ================ ===<反灰>============= 1^2 == 1 2^2 == 4 3^2 == 9 1*2 == 2 2*2 == 4 3*2 == 6 1^2 == 1 2^2 == 4 3^2 == 9 ================