我在讀一本書,書名叫『掌握VC++關鍵技術』,書中提到使用『茴字的N種寫法』:
1. 使用Windows API (WriteFile, ReadFile)
2. 使用標準C++函式庫 (ofstream, ifstream)
3. 使用CRT (fprintf, fscanf)
4. 使用CRT函式庫的寬字串版本 (fwprintf, fwscanf)
5. 使用CRT函式庫的安全版本 (fwprintf_s, fwscanf_s)
6. 使用MFC/ATL (CFile::Write, CFile::Read)
7. 使用C++/CLI (StreamWriter, StreamReader)
各種寫法介紹完之後,作者提到『該採用哪一種寫法』:
。。。因此,本書中我們儘量只介紹本機C++的用法,在本機C++語言中,如果同時又存在著幾種技術實現方法,那麼我們給讀者的建議是:優先採用MFC和Windows API,其次再使用標準C++的類別和CRT函式庫。
書還沒看完,但是,到此,我還是搞不清楚哪些是MFC的東東,哪些又是標準C++函式庫。只有Windows API是看到就知道是這種的而已!
分不清楚什麼是什麼,要怎麼選擇呢?!這個問題或許多學點就會知道,不過,就先把它列在這裡,讓有心得的朋友們給點建議!
底下的問題才是這裡真正要問的。下面的一段程式碼是建立在『Win32主控台程式』並設定支援『MFC/ATL』下寫的。其中,前後各有兩段我用『這是第一種寫法(已被//標示起來當做註解)』及『這是第二種寫法』分隔開來。想知道的是,各位已有寫VC++的朋友們的經驗裡,會選擇哪一種方式來寫?
乍看下,第一種寫法好像是直接用指標操作,是不是在效能方面會較第二種寫法好?!- // 掌握VCPP 3-3.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include "掌握VCPP 3-3.h"
- //這是第一種寫法
- //#include <cstdio>
- //#include <cstring>
- //這是第二種寫法
- #include <iostream>
- #include <string>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 僅有的一個應用程式物件
- CWinApp theApp;
- using namespace std;
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // 初始化 MFC 並於失敗時列印錯誤
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 配合您的需要變更錯誤碼
- _tprintf(_T("嚴重錯誤: MFC 初始化失敗\n"));
- nRetCode = 1;
- }
- else
- {
- // TODO: 在此撰寫應用程式行為的程式碼。
- //這是第一種寫法
- //char src[] = "Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.";
- //char key[] = "aeiou";
- //char *cp;
- //printf("%s\r\n",src);
- //printf("母音子母: ");
- //cp = strpbrk(src,key);
- //int i = 0;
- //while(cp != NULL)
- //{
- // i++;
- // printf("%c ",*cp);
- // cp = strpbrk(cp+1,key);
- //}
- //printf("\r\n共有%d個母音\r\n",i);
- //這是第二種寫法
- string src ("Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.");
- cout << src << endl;
- size_t cp;
- cp = src.find_first_of("aeiou");
- int i = 0;
- while (cp != string::npos)
- {
- cout << src.at(cp) << " ";
- cp = src.find_first_of("aeiou",cp+1);
- i++;
- }
- cout << endl << "共有" << i << "個母音" << endl;
- }
- return nRetCode;
- }
複製代碼 純VC++菜鳥,所問問題可能程度太低,尚請見諒! |