青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

平凡的世界

神鷹忽展翅,頭頂青天飛
隨筆 - 10, 文章 - 0, 評論 - 34, 引用 - 0
數據加載中……

[原][譯] Tips for STL and Generic Programming-關于STL與泛型編程的忠告

原文地址:http://tmh-coding-tips.blogspot.com/2008/09/cc-tips-for-stl-and-generic-programming.html

 

[C/C++] - Tips for STL and Generic Programming

關于STL“泛型”編程的忠告(v1.5

Translated By Phoenix(E-Mail:phoenix8848@gmail.com)

       Tip 15: The Location of Template Definitions

       忠告15:模板定義的位置

       Normally, you declare functions and classes in a .h file and place their definition in a separate .cpp file. With templates, this practice isn't really useful because the compiler must see the actual definition (i.e., the body) of a template, not just its declaration, when it instantiates a template. Therefore, it's best to place both the template's declaration and definition in the same .h file. This is why all STL header files contain template definitions. 
       通常你在.h文件中聲明函數與類而把它們的實現放在若干個.cpp文件中(以使聲明與實現分開)。使用模板的時候這樣的做法作用不大因為編譯器在實例化一個類模板時必須顯式地獲得類模板的實際定義(例如函數體),而不僅僅是它的聲明。所以最好是把類模板的聲明和實現放在同一個頭文件中。這就是為什么所有的STL(標準模板庫,即Standard Template Library,是一個C++軟件庫,也是C++標準程式庫的一部分)頭文件都包含模板的實現。
      
In the future, when compilers support the "export" keyword, it will be possible to use only the template's declaration and leave the definition in a separate source file.

       將來當編譯器支持“export”命令時,有可能只需要模板的聲明而將它的實現放在單獨的源文件中。

       Tip 16: Standard Base Classes for Function Object

       忠告16:函數對象的標準基類

       To simplify the process of writing function objects, the Standard Library provides two class templates that serve as base classes of user-defined function objects: std::unary_function and std::binary_function. Both are declared in the header “functional”. As the names suggest, unary_function serves as a base class of function objects taking one argument and binary_function serves as a base class of function objects taking two arguments. The definitions of these base classes are as follows:

       為了簡便地處理寫入函數對象,標準庫提供了兩個類模板以作為用戶自定義函數對象的基類:std::unary_functionstd::binary_funcation。這兩個模板都聲明在頭文件“functional”中。正如模板名字所示,unary_funcation作為單參數函數對象的基類而binary_function作為雙參數函數對象的基類。這兩個基類的定義如下所示:

 

 1template <class Arg, class Res> 
 2struct unary_function 
 3{
 4    typedef Arg argument_type;
 5    typedef Res result_type;
 6}
;
 7
 8template <class Arg, class Arg2, class Res> 
 9struct binary_function 
10{
11    typedef Arg first_argument_type;
12    typedef Arg2 second_argument_type;
13    typedef Res result_type;
14}
;
15
16

 

       These templates don't provide any useful functionality. They merely ensure that arguments and return values of their derived function objects have uniform names. In the following example, the predicate is_vowel, which takes one argument, inherits from unary_function:

       這兩個模板并沒有提供任何有用的功能。它們僅僅是為了保證派生出來的函數對象的參數與返回值具有不同的名字。在下面的例子中聲明了從unary_function派生的_vowel,它只有一個參數。

 

 1template <class T> 
 2class is_vowel: public unary_function<T, bool>
 3{
 4public:
 5    bool operator ()(T t) const
 6    {
 7        if ((t=='a')||(t=='e')||(t=='i')||(t=='o')||(t=='u'))
 8            return true;
 9        else
10            return false;
11    }

12}
;

 

       Tip 17: Storing Dynamically Allocated Objects in STL Containers

       忠告17:在標準模板庫容器中保存動態生成的對象

       Suppose you need to store objects of different types in the same container. Usually, you do this by storing pointers to dynamically allocated objects. However, instead of using named pointers, insert the elements to the container as follows:

       假設你需要在一個容器中存儲不同的對象。通常你會通過保存動態生成對象的指針來實現。無論如何不要用命名的指針,而是像下面所示的這樣添加元素到容器中:

1class Base {};
2class Derived : public Base{};
3
4std::vector<Base *> v;
5v.push_back(new Derived);
6v.push_back(new Base);



      
This way you ensure that the stored objects can only be accessed through their container. Remember to delete the allocated objects as follows:

       這樣你可以保證存儲的對象只能通過容器訪問到。切記要用下面所示的方法釋放對象:

 

1delete v[0];
2delete v[1];
3

 

       Tip 18: Treating a Vector as an Array

       忠告18:將向量容器看作是數組

       Suppose you have a vector of int and function that takes int *. To obtain the address of the internal array of the vector v and pass it to the function, use the expressions &v[0] or &*v.front(). For example:

       假設你有一個保存整型變量的向量容器和一個處理整型指針int*的函數。為了獲得向量容器v中的內部數組并把它傳遞給函數,應該使用表達式&v[0]或者&*v.front(),如示例:

 1void func(const int arr[], size_t length );
 2
 3int main()
 4{
 5    vector<int> vi;
 6    //.. fill vi
 7    func(&vi[0], vi.size());
 8}

 9
10


       It's safe to use &vi[0] and &*v.front() as the internal array's address as long as you adhere to the following rules: First, func() shouldn't access out-of-range array elements. Second, the elements inside the vector must be contiguous. Although the C++ Standard doesn't guarantee that yet, I'm not aware of any implementation that doesn't use contiguous memory for vectors. Furthermore, this loophole in the C++ Standard will be fixed soon.

       如果你能遵循下面的規則使用&vi[0]&*v.front()作為內部數組的地址是安全的:第一,func()函數不能試圖訪問超出范圍的數組元素。第二,向量容器內的元素必須是連續的。盡管C++標準并不要求如此,但我不知道有別的為容器實現不使用連續內存。而且,C++標準不久就會修補這個漏洞。

       Tip 19: Dynamic Multidimensional Arrays and Vectors

       忠告19:動態多維數組與容器

       You can allocate multidimensional arrays manually, as in:

       通常你會像下面的方式來分配多維數據:

 

 1int (*ppi) [5]=new int[4][5]; /*parentheses required*/
 2
 3/*fill array..*/
 4
 5ppi[0][0= 65;
 6ppi[0][1= 66;
 7ppi[0][2= 67;
 8
 9//..
10
11delete [] ppi;

 

       However, this style is tedious and error prone. You must parenthesize ppi to ensure that the compiler parses the declaration correctly, and you must delete the allocated memory. Worse yet, you can easily bump into buffer overflows. Using a vector of vectors to simulate a multidimensional array is a significantly superior alternative:

       可是這種做法是冗長而且非常容易出錯。你必須把ppi放入括號內以便編譯器可以正確地分析這些聲明,并且(最后)你必須(顯式地)地釋放這些分配的內存空間。更糟糕的是,你會很容易地造成緩沖溢出。使用“容器之容器”來模擬多維數組是一種高明得多的選擇。

 1#include <vector>
 2#include <iostream>
 3using namespace std;
 4
 5int main()
 6{
 7    vector<vector<int>> v; /*two dimensions*/
 8    v.push_back(vector<int>()); /*create v[0]*/
 9    v.push_back(vector<int>()); /*create v[1]*/
10    v[0].push_back(15); /*assign v[0][0]*/
11    v[1].push_back(16); /*assign v[1][0]*/
12}


       Because vector overloads operator [], you can use the [][] notation as if you were using a built-in two-dimensional array:

       因為容器重載了操作符[],所以你可以用[][]來操作就如同使用內置的二維數組。

 

1cout<<v[0][0]; 
2cout<<v[1][0];

 

       The main advantages of using a vector of vectors are two: vector automatically allocates memory as needed. Secondly, it takes care of deallocating memory so you don't have to worry about potential memory leaks.

       使用“容器之容器”有兩個主要的好處:第一,容器會在需要時自動分配內存。第二,容器可以自動回收內存所以你不必操心潛在的內存泄漏。

 

       Tip 20: Why You Shouldn't Store auto_ptr Objects in STL Containers

       忠告20:為什么不能在標準模板庫容器中存儲auto_ptr

       The C++ Standard says that an STL element must be "copy-constructible" and "assignable." These fancy terms basically mean that for a given class, assigning and copying one object to another are well-behaved operations. In particular, the state of the original object isn't changed when you copy it to the target object.

       C++標準中說一個STL元素必須是可復制構造(copy-constructible)和可賦值(assignable)的。這些充滿科幻色彩的詞匯的基本意思是對于一個給定的類,對象間的賦值與復制是一種表現良好的操作。特別的,源對象被復制給一個目標對象時源對象不會有任何改變。

       This is not the case with auto_ptr, though: copying or assigning one auto_ptr to another makes changes to the original in addition to the expected changes in the copy. To be more specific, the original object transfers ownership of the pointer to the target, thus making the pointer in the original null. Imagine what would happen if you did something like this:

       這不適用于auto_ptr。因為,把一個auto_ptr對象復制或賦值給另一個對象會引起原始對象的改變從而引起對象副本的改變。更細節地看,原始對象把指針的權限傳遞給了目標對象,這使源對象中的指針變成空指針。設想當你進行下面這樣操作的時候會發生什么:

 1std::vector<auto_ptr<Foo>> vf;/*a vector of auto_ptr's*/
 2
 3// ..fill vf
 4
 5int g()
 6{
 7    std::auto_ptr<Foo> temp=vf[0]; /*vf[0] becomes null*/
 8}

 9
10


       When temp is initialized, the pointer of vf[0] becomes null. Any attempt to use that element will cause a runtime crash. This situation is likely to occur whenever you copy an element from the container. Remember that even if your code doesn't perform any explicit copy or assignment operations, many algorithms (std::swap(), std::random_shuffle() etc.) create a temporary copy of one or more container elements. Furthermore, certain member functions of the container create a temporary copy of one or more elements, thereby nullifying them. Any subsequent attempt to the container elements is therefore undefined.

       當臨時對象被初始化,vf[0]的指針變為空。任何試圖使用這個元素都會引起運行時崩潰。無論什么時候你從容器復制這個元素都會引發這種意外。切記即使你的代碼不進行任何顯示的復制與賦值操作,很多算法(如std::swap(), std::random_shuffle())都會(隱蔽地)創建一個或更多容器元素的臨時副本。此外,容器的某些特定成員函數也會一個或列多元素的臨時副本,從而使這些元素失效。任何隨后的對容器元素的操作意圖都由此變為不可預期的。

       Visual C++ users often say that they have never encountered any problems with using auto_ptr in STL containers. This is because the auto_ptr implementation of Visual C++ (all versions thereof) is outdated and relies on an obsolete specification. When the vendor decides to catch up with the current ANSI/ISO C++ Standard and change its Standard Library accordingly, code that uses auto_ptr in STL containers will manifest serious malfunctions.

       Visual C++用戶經常說他們在標準模板容器中使用auto_ptr時從沒遇到什么問題。這是因為Visual C++auto_ptr的實現(目前為止所有的版本)都是過時的和參考了舊的規范說明。當微軟決定采用現在的ANSI/ISO C++標準并據此對它的標準庫進行更新時,以前在標準模板容器中使用auto_ptr的代碼就會突現嚴重的問題。

       To conclude, you shouldn't use auto_ptr in STL containers. Use either bare pointers or other smart pointer classes instead of auto_ptr (such classes are available at www.Boost.org).

       作為結論,你最好不要在標準容器中使用auto_ptr,而是使用原始指針或別的智能指針類來代替auto_ptr(比如BOOST庫)

posted on 2008-09-16 03:18 西門有悔 閱讀(1876) 評論(4)  編輯 收藏 引用

評論

# re: [原][譯] Tips for STL and Generic Programming  回復  更多評論   

為什么不把英文弄掉?那樣會更方便閱讀
2008-09-16 11:26 | 肥仔

# re: [原][譯] Tips for STL and Generic Programming[未登錄]  回復  更多評論   

因為作者認為翻譯可能有錯,這個時候可以通過閱讀原文來解決疑問。
2008-09-16 13:10 | 陳梓瀚(vczh)

# re: [原][譯] Tips for STL and Generic Programming-關于STL與一般編程的忠告  回復  更多評論   

那叫……泛型編程
2008-09-18 12:47 | 還要姓名?

# re: [原][譯] Tips for STL and Generic Programming-關于STL與泛型編程的忠告  回復  更多評論   

@還要姓名?

瀑布汗~~~謝謝你。改過來了。
2008-09-24 15:29 | 西門有悔

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品进线69影院| 国产精品高潮久久| 在线成人国产| 欧美电影免费观看网站| 久久夜色精品国产欧美乱极品 | 亚洲高清网站| 媚黑女一区二区| 一区二区三区高清在线观看| 99精品久久| 国产女主播在线一区二区| 久久久91精品国产| 裸体丰满少妇做受久久99精品| 亚洲精品久久久久久久久久久久 | 美女黄毛**国产精品啪啪| 久久免费一区| 9色国产精品| 亚洲免费视频观看| 亚洲高清av| 一本色道久久综合亚洲精品不卡| 国产女优一区| 欧美激情中文不卡| 国产精品亚洲综合色区韩国| 久久这里有精品15一区二区三区| 欧美xart系列在线观看| 亚洲欧美视频在线观看视频| 久久久xxx| 亚洲一区二区三区国产| 欧美中文字幕视频| 亚洲少妇诱惑| 久久蜜臀精品av| 亚洲欧美中文日韩v在线观看| 久久久久国色av免费看影院| 在线一区免费观看| 噜噜噜噜噜久久久久久91 | 国产亚洲毛片| 亚洲另类一区二区| 影音先锋欧美精品| 亚洲欧美精品在线观看| 亚洲另类自拍| 久久久久久久久综合| 亚洲欧美激情在线视频| 毛片一区二区三区| 久久精品综合| 国产精品久久久久久久久久免费| 欧美黄在线观看| 国产在线欧美| 午夜激情综合网| 亚洲无人区一区| 欧美成人激情视频| 免费永久网站黄欧美| 国产拍揄自揄精品视频麻豆| 一本色道久久综合精品竹菊| 亚洲精品日韩在线| 欧美成年人视频| 男人插女人欧美| 国产在线观看精品一区二区三区| 亚洲一区二区三区视频| 亚洲小说春色综合另类电影| 欧美激情综合在线| 亚洲国产毛片完整版| 亚洲精品一品区二品区三品区| 国产一区91精品张津瑜| 亚洲自拍偷拍麻豆| 欧美一区=区| 国产精品毛片a∨一区二区三区|国| 最新日韩欧美| 亚洲精品护士| 欧美精品九九| 日韩亚洲一区二区| 亚洲视频在线观看免费| 欧美日韩国产一区| 一区二区三区四区国产| 亚洲欧美激情四射在线日 | 久久久亚洲精品一区二区三区| 欧美一二三视频| 国产日韩精品久久| 久久成人人人人精品欧| 久久综合久久久久88| 在线不卡a资源高清| 免费高清在线一区| 亚洲经典视频在线观看| 中日韩高清电影网| 国产精品日韩欧美大师| 欧美一区二区三区久久精品茉莉花| 久久久99精品免费观看不卡| 国语自产偷拍精品视频偷| 久久久91精品国产| 亚洲国产精品尤物yw在线观看| 一本色道婷婷久久欧美| 欧美视频不卡中文| 亚洲欧美一级二级三级| 蜜臀a∨国产成人精品| 亚洲日本久久| 国产精品私房写真福利视频| 久久久久亚洲综合| 亚洲精品乱码| 久久精品免费播放| 亚洲精品乱码久久久久| 国产精品久久久久91| 久久久不卡网国产精品一区| 亚洲激情电影中文字幕| 欧美一区二区在线免费观看| 在线免费观看日本一区| 欧美天天在线| 久久久久久九九九九| 日韩午夜三级在线| 久热精品在线视频| 亚洲影院色在线观看免费| 激情文学综合丁香| 国产精品成人观看视频免费 | 91久久国产综合久久蜜月精品| 亚洲一区二区免费视频| 韩国av一区二区三区在线观看| 欧美韩日一区二区三区| 欧美一级专区| 9i看片成人免费高清| 久久久一二三| 亚洲一区二区三区精品视频| 在线视频国产日韩| 国产欧美日韩高清| 欧美日韩国产成人在线91| 欧美在线欧美在线| 亚洲一区精品在线| 日韩午夜视频在线观看| 亚洲国产一区二区三区a毛片| 久久久久成人精品| 午夜一级久久| 亚洲一区免费在线观看| 亚洲乱码国产乱码精品精98午夜 | 欧美激情一区二区三区四区 | 欧美激情精品久久久久| 久久久噜噜噜久久人人看| 亚洲一区图片| 亚洲天堂av高清| 一二美女精品欧洲| 亚洲美女淫视频| 亚洲日本国产| 亚洲毛片一区二区| 欧美自拍丝袜亚洲| 亚洲欧洲在线播放| 欧美777四色影视在线| 久久精品一区二区| 小处雏高清一区二区三区| 亚洲午夜一区二区| 亚洲美女av电影| 亚洲久久在线| 亚洲看片免费| 夜夜嗨av一区二区三区中文字幕| 亚洲每日更新| 一本色道久久综合| 亚洲一区二区三区在线看| 一区二区三区成人精品| 亚洲一二三级电影| 欧美一区二区三区免费大片| 欧美一区二区三区喷汁尤物| 亚洲欧美中文字幕| 久久精品99无色码中文字幕| 久久久夜色精品亚洲| 久久综合九色99| 欧美日韩福利在线观看| 欧美亚洲第一区| 国产精品视频yy9099| 国产无一区二区| 1769国内精品视频在线播放| 亚洲九九精品| 欧美一区二区三区精品| 久久夜色精品国产欧美乱极品| 欧美电影电视剧在线观看| 亚洲美女av电影| 亚洲欧洲av一区二区| 久久夜色撩人精品| 欧美日韩亚洲激情| 狠狠爱www人成狠狠爱综合网| 在线观看亚洲精品视频| 日韩亚洲精品视频| 篠田优中文在线播放第一区| 老司机午夜精品视频| 日韩亚洲欧美在线观看| 性一交一乱一区二区洋洋av| 免费在线亚洲| 国产精品三区www17con| 在线看欧美日韩| 亚洲欧美日本日韩| 欧美国产三级| 亚洲欧美日韩国产成人| 免费观看日韩| 国产亚洲一级| 中文精品99久久国产香蕉| 久久男女视频| 日韩网站在线观看| 久久在线免费| 国产欧美精品国产国产专区| 亚洲国产片色| 久久久久免费观看| 亚洲免费电影在线| 欧美freesex8一10精品| 国产亚洲精品久久飘花| 亚洲一区视频| 亚洲人成人一区二区三区| 久久久久www|