<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                創建新類型時,第一步一般是聲明實例變量并編寫構造函數,第二步一般是編寫一個可以將對象以可讀形式打印出來的函數。 對于紙牌的情況,“可讀”指的是我們必須將大小和花色的內部表示映射為單詞。一種自然的方法是使用apstring的向量完成該功能。你可以像創建其他類型的向量一樣創建apstring的向量: ~~~ apvector<apstring> suits (4); ~~~ 當然,為了使用apvector和apstring類型,必須包含它們的頭文件【注】。 為了初始化向量的元素,我們可以使用一系列賦值語句: ~~~ suits[0] = "Clubs"; suits[1] = "Diamonds"; suits[2] = "Hearts"; suits[3] = "Spades"; ~~~ 這個向量的狀態圖如下所示:?![enter image description here](https://box.kancloud.cn/2015-09-02_55e646be5bd68.jpg) 我們可以構建一個類似的向量來解碼牌的大小。然后,我們就能以花色和大小為索引選擇適當的元素了。最后,我們能夠編寫print函數來輸出調用該函數的紙牌的信息: ~~~ void Card::print () const { apvector<apstring> suits (4); suits[0] = "Clubs"; suits[1] = "Diamonds"; suits[2] = "Hearts"; suits[3] = "Spades"; apvector<apstring> ranks (14); ranks[1] = "Ace"; ranks[2] = "2"; ranks[3] = "3"; ranks[4] = "4"; ranks[5] = "5"; ranks[6] = "6"; ranks[7] = "7"; ranks[8] = "8"; ranks[9] = "9"; ranks[10] = "10"; ranks[11] = "Jack"; ranks[12] = "Queen"; ranks[13] = "King"; cout << ranks[rank] << " of " << suits[suit] << endl; } ~~~ 表達式suits[suit]的意義是“以當前對象的實例變量suit為索引從向量suits選擇適當的字符串”。 因為print是Card類的成員函數,所以它能隱式地(即不適用點記法指定對象)引用當前對象的實例變量。比如下面代碼: ~~~ Card card (1, 11); card.print (); ~~~ 其輸出是“Jack of Diamonds”。 你可能注意到了,我們沒有使用表示牌大小的向量的第0個元素。那是因為只有1-13之間的牌大小值才是有效的。通過在向量的開頭留下一個未用元素,我們得到了從2映射到“2”,3映射到“3”等這樣的編碼。從用戶的觀點看,編碼是什么并不重要,因為所有的輸入和輸出都是用可讀的形式表示的。 另一方面,如果映射易于記憶,這對程序員來說是有幫助的。 *注:apvectors are a little different from apstrings in this regard. The file apvector.cpp contains a template that allows the compiler to create vectors of various kinds. The first time you use a vector of integers, the compiler generates code to support that kind of vector. If you use a vector of apstrings, the compiler generates different code to handle that kind of vector. As a result, it is usually sufficient to include the header file apvector.h; you do not have to compile apvector.cpp at all! Unfortunately, if you do, you are likely to get a long stream of error messages. I hope this footnote helps you avoid an unpleasant surprise, but the details in your development environment may differ.*!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看