亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
奇米影视在线99精品| 日韩电影免费在线观看网站| 日韩精品中文字幕一区二区三区 | 老鸭窝一区二区久久精品| 欧美精品亚洲二区| 色婷婷综合五月| 91影视在线播放| 欧美在线视频全部完| 在线免费观看不卡av| 日本韩国一区二区| 欧美日韩高清一区二区不卡| 欧美性生活一区| 在线播放中文一区| 日韩久久久久久| 国产调教视频一区| 亚洲人妖av一区二区| 亚洲国产日韩a在线播放性色| 日本特黄久久久高潮| 精彩视频一区二区| www.亚洲激情.com| 欧美人伦禁忌dvd放荡欲情| 欧美一区二区三区日韩视频| 久久综合久色欧美综合狠狠| 国产精品久久免费看| 一区二区三区欧美在线观看| 日本午夜一本久久久综合| 韩国视频一区二区| 成人av影院在线| 欧美欧美欧美欧美首页| 久久久久久免费毛片精品| 自拍偷拍国产精品| 美美哒免费高清在线观看视频一区二区 | 视频一区中文字幕国产| 久久福利资源站| 色综合久久久久久久久久久| 欧美年轻男男videosbes| 精品福利在线导航| 亚洲国产综合人成综合网站| 看电视剧不卡顿的网站| 成人影视亚洲图片在线| 欧美另类高清zo欧美| 久久久久久免费毛片精品| 亚洲精品一二三四区| 久久 天天综合| 一本一道综合狠狠老| 精品粉嫩aⅴ一区二区三区四区 | 欧美三级乱人伦电影| 久久九九久久九九| 麻豆精品一区二区av白丝在线| 国产成人精品影院| 欧美一二三区在线观看| 亚洲视频1区2区| 东方aⅴ免费观看久久av| 在线不卡中文字幕播放| 一级特黄大欧美久久久| 国产成人精品一区二| 91精品国产综合久久精品图片| 国产精品美女久久久久久久久| 久久国产精品99精品国产| 精品视频一区三区九区| 亚洲三级久久久| av影院午夜一区| 国产午夜久久久久| 韩国三级电影一区二区| 日韩三级在线观看| 天堂一区二区在线| 欧美三级中文字| 亚洲第一狼人社区| 欧美综合亚洲图片综合区| 一区二区三区小说| 色成人在线视频| 亚洲男帅同性gay1069| 99久久久国产精品| 国产精品乱人伦| av电影在线观看一区| 中文字幕中文字幕一区二区| 成人黄色在线看| 国产精品伦理一区二区| 成人av网站在线观看| 国产精品电影院| www.色综合.com| 中文字幕亚洲成人| 色综合天天综合在线视频| 亚洲永久免费av| 在线播放中文字幕一区| 蜜乳av一区二区三区| 51精品秘密在线观看| 麻豆精品视频在线观看| 久久久电影一区二区三区| 国产91精品免费| 亚洲日韩欧美一区二区在线| 色老头久久综合| 强制捆绑调教一区二区| 国产婷婷色一区二区三区在线| 成人禁用看黄a在线| 亚洲精品欧美专区| 欧美一级黄色录像| 高清不卡在线观看| 一区二区三区自拍| 欧美一级电影网站| 不卡av在线免费观看| 亚洲在线中文字幕| 欧美电视剧免费全集观看| 国产成人免费网站| 亚洲国产精品久久久久婷婷884| 91精品国产色综合久久| 国产精品99久久久久久久vr| 亚洲精品欧美二区三区中文字幕| 91精品国产入口| 波多野结衣精品在线| 日韩1区2区3区| 中文字幕第一区二区| 欧美日韩一区二区电影| 国产在线观看免费一区| 亚洲日本乱码在线观看| 精品美女在线观看| 一本色道**综合亚洲精品蜜桃冫| 久久99久久99精品免视看婷婷| 中文字幕中文字幕在线一区| 日韩片之四级片| 91免费国产在线| 国产一区二区按摩在线观看| 亚洲国产成人91porn| 国产精品久久久久婷婷| 日韩一区二区影院| 欧美自拍偷拍一区| 成人一区二区三区在线观看| 人妖欧美一区二区| 亚洲伊人伊色伊影伊综合网| 国产精品视频一区二区三区不卡| 在线综合+亚洲+欧美中文字幕| 99久久久精品| 成人免费视频caoporn| 韩国一区二区视频| 美女一区二区三区在线观看| 亚洲一区二区综合| 中文字幕中文字幕一区二区| 国产女人18水真多18精品一级做| 91麻豆精品国产91久久久久久 | 在线观看视频一区二区| 成人美女视频在线观看18| 精品一区二区三区在线播放视频| 亚洲一区二区精品视频| 亚洲精品乱码久久久久久久久 | 日本精品视频一区二区| 成人h精品动漫一区二区三区| 精品一区中文字幕| 老司机一区二区| 麻豆专区一区二区三区四区五区| 午夜精品久久一牛影视| 亚洲成人动漫精品| 亚洲国产成人va在线观看天堂| 亚洲激情五月婷婷| 亚洲乱码国产乱码精品精小说| 亚洲国产精品精华液2区45| 国产午夜三级一区二区三| 国产欧美日韩另类一区| 国产亚洲一二三区| 国产精品国产自产拍在线| 国产欧美精品国产国产专区 | 91在线观看免费视频| av综合在线播放| 91精彩视频在线观看| 欧美视频在线一区二区三区 | 2023国产一二三区日本精品2022| 欧美精品一区二区不卡| 久久综合久久99| 亚洲欧洲日韩av| 亚洲一区在线观看视频| 日韩国产一区二| 国产成人免费视频网站| 91女神在线视频| 欧美午夜一区二区三区免费大片| 欧美日韩的一区二区| 精品嫩草影院久久| 国产精品白丝在线| 午夜成人在线视频| 国产麻豆成人精品| 91丨porny丨国产| 欧美一区二区三区成人| 欧美激情一区二区三区四区| 一个色在线综合| 韩国精品免费视频| 色又黄又爽网站www久久| 日韩精品一区二区三区在线观看 | 欧美亚洲动漫精品| 精品久久久久久久人人人人传媒| 中文字幕成人网| 亚洲国产日韩av| 国产一区二区不卡老阿姨| 在线观看视频欧美| 国产日本欧美一区二区| 五月婷婷综合激情| 粉嫩aⅴ一区二区三区四区五区| 欧美少妇bbb| 国产精品另类一区| 久久疯狂做爰流白浆xx| 在线精品国精品国产尤物884a| 精品国产乱码久久久久久图片 | 色拍拍在线精品视频8848|