亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? chapter13.html

?? think like a computer scientist
?? HTML
?? 第 1 頁 / 共 2 頁
字號:
<HTML><HEAD>  <TITLE>Chapter 13</TITLE>  <LINK REL="STYLESHEET" HREF="downey.css" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/downey.css"></HEAD><BODY><H2>Chapter 13</H2><H1>Objects of Vectors</H1><H3>13.1 Enumerated types</H3><P>In the previous chapter I talked about mappings between real-world values like rank and suit, and internal representations like integers and strings. Although we created a mapping between ranks and integers, and between suits andintegers, I pointed out that the mapping itself does not appear as part of theprogram.</P><P>Actually, C++ provides a feature called and <B>enumerated type</B> that makes it possible to (1) include a mapping as part of the program, and (2) define the set of values that make up the mapping. For example, here is the definition of the enumerated types <TT>Suit</TT> and <TT>Rank</TT>:</P><PRE>enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };enum Rank { ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,TEN, JACK, QUEEN, KING };</PRE><P>By default, the first value in the enumerated type maps to 0, the second to 1, and so on. Within the <TT>Suit</TT> type, the value <TT>CLUBS</TT> is represented by the integer 0, <TT>DIAMONDS</TT> is represented by 1, etc.</P><P>The definition of <TT>Rank</TT> overrides the default mapping and specifies that <TT>ACE</TT> should be represented by the integer 1. The other values follow in the usual way.</P><P>Once we have defined these types, we can use them anywhere. For example, theinstance variables <TT>rank</TT> and <TT>suit</TT> are can be declared with type <TT>Rank</TT> and <TT>Suit</TT>:</P><PRE>struct Card{  Rank rank;  Suit suit;  Card (Suit s, Rank r);};</PRE><P>That the types of the parameters for the constructor have changed, too. Now,to create a card, we can use the values from the enumerated type as arguments:</P><PRE>  Card card (DIAMONDS, JACK);</PRE><P>By convention, the values in enumerated types have names with all capital letters. This code is much clearer than the alternative using integers:</P><PRE>  Card card (1, 11);</PRE><P>Because we know that the values in the enumerated types are represented as integers, we can use them as indices for a vector. Therefore the old <TT>print</TT> function will work without modification. We have to make some changes in <TT>buildDeck</TT>, though:</P><PRE>  int index = 0;  for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) {    for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) {      deck[index].suit = suit;      deck[index].rank = rank;      index++;    }  }</PRE><P>In some ways, using enumerated types makes this code more readable, but there is one complication. Strictly speaking, we are not allowed to do arithmetic with enumerated types, so <TT>suit++</TT> is not legal. On the otherhand, in the expression <TT>suit+1</TT>, C++ automatically converts the enumerated type to integer. Then we can take the result and typecast it back tothe enumerated type:</P><PRE>  suit = Suit(suit+1);  rank = Rank(rank+1);</PRE><P>Actually, there is a better way to do this---we can define the <TT>++</TT> operator for enumerated types---but that is beyond the scope of this book.</P><BR><BR><H3><TT>switch</TT> statement</H3><P>It's hard to mention enumerated types without mentioning <TT>switch</TT> statements, because they often go hand in hand.  A <TT>switch</TT> statementis an alternative to a chained conditional that is syntactically prettier and often more efficient. It looks like this:</P><PRE>  switch (symbol) {  case '+':    perform_addition ();    break;  case '*':    perform_multiplication ();    break;  default:    cout << "I only know how to perform addition and multiplication" << endl;    break;  }</PRE><P>This <TT>switch</TT> statement is equivalent to the following chainedconditional:</P><PRE>  if (symbol == '+') {    perform_addition ();  } else if (symbol == '*') {    perform_multiplication ();  } else {    cout << "I only know how to perform addition and multiplication" << endl;  }</PRE><P>The <TT>break</TT> statements are necessary in each branch in a <TT>switch</TT> statement because otherwise the flow of execution ``falls through'' to the next case. Without the <TT>break</TT> statements, the symbol <TT>+</TT> would make the program perform addition, and then perform multiplication, and then print the error message. Occasionally this feature is useful, but most of the time it is a source of errors when people forget the <TT>break</TT> statements.</P><P><TT>switch</TT> statements work with integers, characters, and enumerated types. For example, to convert a <TT>Suit</TT> to the corresponding string, we could use something like:</P><PRE>  switch (suit) {  case CLUBS:     return "Clubs";  case DIAMONDS:  return "Diamonds";  case HEARTS:    return "Hearts";  case SPADES:    return "Spades";  default:        return "Not a valid suit";  }</PRE><P>In this case we don't need <TT>break</TT> statements because the <TT>return</TT> statements cause the flow of execution to return to the caller instead of falling through to the next case.</P><P>In general it is good style to include a <TT>default</TT> case in every <TT>switch</TT> statement, to handle errors or unexpected values.</P><BR><BR><H3>13.3 Decks</H3><P>In the previous chapter, we worked with a vector of objects, but I also mentioned that it is possible to have an object that contains a vector as an instance variable. In this chapter I am going to create a new object, called a <TT>Deck</TT>, that contains a vector of <TT>Card</TT>s.</P><P>The structure definition looks like this</P><PRE>struct Deck {  apvector<Card> cards;  Deck (int n);};Deck::Deck (int size){  apvector<Card> temp (size);  cards = temp;}</PRE><P>The name of the instance variable is <TT>cards</TT> to help distinguish the <TT>Deck</TT> object from the vector of <TT>Card</TT>s that it contains.</P><P>For now there is only one constructor. It creates a local variable named <TT>temp</TT>, which it initializes by invoking the constructor for the <TT>apvector</TT> class, passing the size as a parameter. Then it copies the vector from <TT>temp</TT> into the instance variable <TT>cards</TT>.</P><P>Now we can create a deck of cards like this:</P><PRE>  Deck deck (52);</PRE><P>Here is a state diagram showing what a <TT>Deck</TT> object looks like:</P><P CLASS=1><IMG SRC="images/deckobject.png" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/deckobject.png"></P><P>The object named <TT>deck</TT> has a single instance variable named <TT>cards</TT>, which is a vector of <TT>Card</TT> objects. To access the cardsin a deck we have to compose the syntax for accessing an instance variable and the syntax for selecting an element from an array. For example, the expression <TT>deck.cards[i]</TT> is the ith card in the deck, and <TT>deck.cards[i].suit</TT> is its suit. The following loop</P><PRE>  for (int i = 0; i<52; i++) {    deck.cards[i].print();  }</PRE><P>demonstrates how to traverse the deck and output each card.</P><BR><BR><H3>13.4 Another constructor</H3><P>Now that we have a <TT>Deck</TT> object, it would be useful to initialize the cards in it. From the previous chapter we have a function called <TT>buildDeck</TT> that we could use (with a few adaptations), but it might be more natural to write a second <TT>Deck</TT> constructor.</P><PRE>Deck::Deck (){  apvector<Card> temp (52);  cards = temp;  int i = 0;  for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) {    for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) {      cards[i].suit = suit;      cards[i].rank = rank;      i++;    }  }}</PRE><P>Notice how similar this function is to <TT>buildDeck</TT>, except that we had to change the syntax to make it a constructor. Now we can create a standard52-card deck with the simple declaration <TT>Deck deck;</TT></P><BR><BR><H3>13. 5 <TT>Deck</TT> member functions</H3>Now that we have a <TT>Deck</TT> object, it makes sense to put all the functions that pertain to <TT>Deck</TT>s in the <TT>Deck</TT> structure definition. Looking at the functions we have written so far, one obvious candidate is <TT>printDeck</TT> (Section~\ref{printdeck</TT>). Here's how it looks, rewritten as a <TT>Deck</TT> member function:</P><PRE>void Deck::print () const {  for (int i = 0; i < cards.length(); i++) {    cards[i].print ();  }}</PRE><P>As usual, we can refer to the instance variables of the current object without using dot notation.</P><P>For some of the other functions, it is not obvious whether they should be member functions of <TT>Card</TT>, member functions of <TT>Deck</TT>, or nonmember functions that take <TT>Card</TT>s and <TT>Deck</TT>s as parameters.For example, the version of <TT>find</TT> in the previous chapter takes a <TT>Card</TT> and a <TT>Deck</TT> as arguments, but you could reasonably make it a member function of either type.  As an exercise, rewrite <TT>find</TT> as a <TT>Deck</TT> member function that takes a <TT>Card</TT> as a parameter.</P><P>Writing <TT>find</TT> as a <TT>Card</TT> member function is a little tricky.Here's my version:</P> <PRE>int Card::find (const Deck& deck) const {  for (int i = 0; i &lt; deck.cards.length(); i++) {    if (equals (deck.cards[i], *this)) return i;  }  return -1;}</PRE><P>The first trick is that we have to use the keyword <TT>this</TT> to refer tothe <TT>Card</TT> the function is invoked on.</P><P>The second trick is that C++ does not make it easy to write structure definitions that refer to each other. The problem is that when the compiler is reading the first structure definition, it doesn't know about the second one yet.</P><P>One solution is to declare <TT>Deck</TT> before <TT>Card</TT> and then define <TT>Deck</TT> afterwards:</P><PRE>// declare that Deck is a structure, without defining itstruct Deck;// that way we can refer to it in the definition of Cardstruct Card{  int suit, rank;  Card ();  Card (int s, int r);  void print () const;  bool isGreater (const Card& c2) const;  int find (const Deck& deck) const;};// and then later we provide the definition of Deckstruct Deck {  apvector<Card> cards;  Deck ();  Deck (int n);  void print () const;  int find (const Card& card) const;};</PRE><BR><BR><H3>13.6 Shuffling</H3><P>For most card games you need to be able to shuffle the deck; that is, put the cards in a random order. In Section 10.5 we saw how to generate random numbers, but it is not obvious how to use them to shuffle a deck.</P><P>One possibility is to model the way humans shuffle, which is usually by dividing the deck in two and then reassembling the deck by choosing alternately

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产精品久久久久久福利一牛影视| 色狠狠综合天天综合综合| 日韩精品专区在线影院观看| 奇米色777欧美一区二区| 日韩美女主播在线视频一区二区三区| 日本成人中文字幕| 欧美精品一区二区三区视频| 国产精品一区在线观看你懂的| 久久久久久9999| 99久久99久久精品国产片果冻| 亚洲视频一区在线观看| 欧美亚洲动漫精品| 免费观看久久久4p| 国产欧美一区二区精品仙草咪| 成人蜜臀av电影| 亚洲国产成人精品视频| 日韩精品中午字幕| 不卡的av在线播放| 亚洲一区二区三区四区五区黄| 欧美一区欧美二区| 成人一区二区三区在线观看 | 99精品视频在线观看免费| 亚洲美女电影在线| 日韩欧美区一区二| 99精品视频免费在线观看| 日韩中文字幕1| 国产精品色哟哟| 9191成人精品久久| 不卡av免费在线观看| 午夜亚洲国产au精品一区二区| 精品成人a区在线观看| 一本色道久久综合亚洲aⅴ蜜桃| 三级欧美在线一区| 国产精品久久综合| 欧美一级一区二区| 一本大道综合伊人精品热热| 久久精品99国产精品日本| 亚洲人一二三区| 精品日韩一区二区三区免费视频| 99精品一区二区| 国产尤物一区二区在线| 亚洲国产精品久久人人爱蜜臀| 久久婷婷一区二区三区| 欧美精品少妇一区二区三区| 成人精品视频网站| 久久精品免费观看| 亚洲一区二区三区中文字幕| 欧美高清在线一区二区| 欧美刺激脚交jootjob| 国产精品网站导航| 欧美一区二区三区四区在线观看| 91美女福利视频| 国产99一区视频免费| 免费亚洲电影在线| 亚洲午夜视频在线观看| 亚洲日本成人在线观看| 国产欧美日韩久久| www欧美成人18+| 日韩欧美高清dvd碟片| 欧美人与禽zozo性伦| 在线亚洲+欧美+日本专区| 成人动漫精品一区二区| 国产精品一区在线观看乱码| 男女视频一区二区| 日韩国产在线一| 亚洲va欧美va人人爽午夜| 一区二区在线观看免费视频播放| 中文字幕免费观看一区| 久久精子c满五个校花| 精品日韩在线观看| 精品久久久久av影院| 欧美高清精品3d| 欧美精选午夜久久久乱码6080| 色噜噜久久综合| 在线观看区一区二| 欧美在线观看一二区| 欧美性生交片4| 欧美三级韩国三级日本三斤| 欧美日韩一区二区三区不卡| 欧美亚洲免费在线一区| 欧美日韩久久一区二区| 欧美日本不卡视频| 欧美一区二区三区免费| 精品欧美一区二区三区精品久久| 欧美成人午夜电影| 久久久久99精品国产片| 国产精品美女一区二区在线观看| 亚洲国产精品二十页| 国产精品不卡在线观看| 亚洲欧美色图小说| 天堂午夜影视日韩欧美一区二区| 日本网站在线观看一区二区三区| 蜜臀久久99精品久久久久久9 | 欧美色窝79yyyycom| 欧美三级视频在线观看| 欧美一区二区免费视频| 久久久久亚洲蜜桃| 国产精品久久久99| 午夜欧美在线一二页| 精品一区精品二区高清| 国产成人午夜电影网| 91在线观看高清| 3d动漫精品啪啪一区二区竹菊| 日韩欧美区一区二| 成人免费一区二区三区视频 | 99久久免费国产| 欧美亚洲动漫制服丝袜| 日韩精品中文字幕一区二区三区| 国产色产综合色产在线视频| 亚洲欧美一区二区三区久本道91| 午夜精品一区二区三区免费视频| 久久精品国产秦先生| av不卡免费在线观看| 欧美日韩国产色站一区二区三区| 精品国产乱码久久久久久蜜臀| 国产精品久久久一区麻豆最新章节| 亚洲小说春色综合另类电影| 国产综合久久久久久鬼色| 色天天综合色天天久久| 亚洲精品在线免费播放| 亚洲免费观看高清| 极品少妇一区二区| 在线免费一区三区| 久久久久久久久伊人| 亚洲成人在线观看视频| 成人综合在线网站| 91超碰这里只有精品国产| 国产精品美女视频| 日本不卡免费在线视频| 一本色道久久综合狠狠躁的推荐| 日韩欧美一级二级| 国产999精品久久久久久| 欧美酷刑日本凌虐凌虐| 中文字幕一区二区三区乱码在线 | 经典三级在线一区| 精品视频一区三区九区| 国产精品视频yy9299一区| 日本不卡123| 欧美性大战久久久久久久| 欧美激情一区不卡| 蜜臀av性久久久久蜜臀aⅴ流畅| 色视频欧美一区二区三区| 国产欧美一区二区三区在线老狼| 日韩av网站免费在线| 欧美亚洲丝袜传媒另类| 中文字幕视频一区| 国产白丝精品91爽爽久久| 日韩一级视频免费观看在线| 一区二区国产盗摄色噜噜| 成人av午夜电影| 日本一区二区三区四区在线视频| 另类人妖一区二区av| 91精品国产一区二区三区香蕉| 亚洲一区二区三区美女| 色香蕉成人二区免费| 国产精品久久久久久福利一牛影视 | 欧美刺激脚交jootjob| 亚洲成精国产精品女| 色噜噜狠狠成人中文综合| 国产精品美女久久久久高潮| 国产精品一区二区久激情瑜伽 | 国产999精品久久久久久| 久久色.com| 国产精品影视在线观看| 久久久久久久久久久电影| 久久99国产精品尤物| 日韩欧美一区二区视频| 日本午夜一本久久久综合| 在线91免费看| 久久精品国产免费| 精品国偷自产国产一区| 美女在线观看视频一区二区| 日韩一区二区三区高清免费看看| 强制捆绑调教一区二区| 欧美成人r级一区二区三区| 精品在线一区二区三区| 久久婷婷久久一区二区三区| 国产成人精品1024| 亚洲天堂成人在线观看| 色猫猫国产区一区二在线视频| 一区二区三区91| 91精品国产综合久久精品麻豆| 蜜臀va亚洲va欧美va天堂| 日韩你懂的在线播放| 91精品国产综合久久蜜臀| 另类小说综合欧美亚洲| 久久久久国产精品免费免费搜索| 风流少妇一区二区| 亚洲免费观看高清完整| 欧美剧在线免费观看网站| 久久99最新地址| 国产精品伦一区| 欧美制服丝袜第一页| 蜜桃一区二区三区四区| 国产区在线观看成人精品| 日本高清不卡aⅴ免费网站| 日韩精品色哟哟| 国产欧美日韩在线| 欧美日韩国产乱码电影| 国产精品一区二区男女羞羞无遮挡 |