#副標= Visual Studio 2010開發平台(5) #大標=Visual C++ 2010新功能體驗 #作者=文/王寧疆 ===========box 範例1========= #include #include #include #include using namespace std; int main() { vector v; //宣告型態為vector集合的變數 for (int i = 0; i < 10; ++i) { v.push_back(i); //將1~10的數字放入vector集合中 } for_each(v.begin(), v.end(), [](int n){cout << n << " ";}); //利用for_each迴圈和lambda運算式印出vector集合的內容 cout << endl; } =============end============= ===========box 範例2========= auto p = new int; //p is int* =============end============= ===========box 範例3========= #include #include #include #include #include using namespace std; int main() { vector v; //宣告型態為vector集合的變數 for (int i = 0; i < 10; ++i) { stringstream ss; //宣告stringstream類別的變數 ss< #include using namespace std; int main() { string s = string("h") + "e" + "ll" + "o"; cout << s << endl; } =============end============= ===========box 範例7========= template void Swap(T& a, T& b) { T tmp(a); // 複製a變數的內容 a = b; // 複製b變數的內容 b = tmp; // 複製tmp變數的內容 } =============end============= ===========box 範例8========= template void Swap(T& a, T& b) { T tmp(std::move(a)); a = std::move(b); b = std::move(tmp); } =============end============= ===========box 範例9========= template struct MyArray { int index; int *arr; MyArray() { index=0; arr=new int[N]; } static_assert(N > 0 && N <=100, "Array Size Must between 1~100!"); //限制template參數的範圍的static_assert void AddItem(int i) { if (index < N) arr[index++]=i; } }; int main() { MyArray<1> array1; MyArray<300> array2; //這一行程式碼無法建置成功,因為template的參數的內容值超出合法的範圍 } =============end============= ===========box 範例10========= class A { public: void f(char c){} }; int main() { int * n= nullptr; //宣告n指標的初值為nullptr if (n==nullptr) //比較n指標的內容值是否等於nullptr { //n指標尚未指向任何記憶體位址 } void (A::*pmf)(char)=&A::f; //宣告pmf函數指標指向A類別的f方法 pmf=nullptr; //將pmf函數指標的位址指派成nullptr } =============end============= ===========box 範例11========= #include int main() { int x[10];   std::iota(x, x+10, 5);   //從數值5開始,填入10個數字到x陣列 } =============end============= ===========box 範例12========= virtual BOOL OnGestureZoom (CPoint ptCenter, long lDelta); virtual BOOL OnGesturePan (CPoint ptFrom, CPoint ptTo); virtual BOOL OnGestureRotate (CPoint ptCenter, double dblAngle); virtual BOOL OnGesturePressAndTap (CPoint ptFirstFinger, long lDelta); virtual BOOL OnGestureTwoFingerTap (CPoint ptCenter); =============end============= ===========box 範例13========= CString message("My message to the user"); //欲顯示在Task Dialog上的文字 CString dialogTitle("My Task Dialog title"); //欲顯示在Task Dialog標題列上的文字 CString emptyString; //空白文字 if (CTaskDialog::IsSupported()) //判斷系統是否支援Task Dialog {    CTaskDialog taskDialog(message, emptyString, dialogTitle, TDCBF_OK_BUTTON); //建立CTaskDialog類別的物件     taskDialog.SetMainIcon(MAKEINTRESOURCE(IDR_MAINFRAME));      //設定Task Dialog顯示的圖示 taskDialog.DoModal();//顯示Task Dialog } else //如果系統未支援Task Dialog {  AfxMessageBox(message);  //則利用AfxMessageBox顯示畫面和使用者溝通 } =============end============= ===========box 特別注意======= 利用CTaskDialog類別顯示來和使用者溝通的Task Dialog是Windows Vista或更高版本的作業系統才支援的功能,所以應用程式必須先利用CTaskDialog類別的IsSupported方法判斷應用程式執行所在的電腦是否支援Task Dialog?確定有支援Task Dialog才顯示Task Dialog和使用者溝通,否則就利用AfxMessageBox函數顯示和使用者溝通的畫面。 ==============END============= ===========box 範例14========= CDataRecoveryHandler* autohandler = AfxGetApp()->GetDataRecoveryHandler(); autohandler->SetAutosaveInterval(60000); =============end============= ============box 提示========= 重新啟動管理員不僅能夠在應用程式執行發生未預期的錯誤而必須結束執行時萬無一失地儲存使用者編輯的資料,也可以協助提供自動更新機制的應用程式在自動更新成最新版本的時候,自動重新啟動,但是仍然保有使用者編輯過的文件內容。 ================END=============== ===========box 範例15========= mainFrm->SetTaskbarOverlayIcon(圖示檔案的ID, _T("Info")); =============end============= ==============box 提示========= 在支援Windows Vista或更高的作業系統的工作列的功能方面,程式設計師還可以利用CJumpList類別新增跳躍清單項目到使用者使用滑鼠的右鍵點中工作列中應用程式圖示顯示出來供使用者點選的工作清單中,方便使用者快速選取。 ================END============