C++複合類型(結構體)

來源:互聯網
上載者:User

標籤:賦值   closed   .com   分享   with   logs   false   ges   exp   

其實c++的結構體可以理解為類似於python的字典,我個人理解, 但有區別

先看結構

#include <iostream>關鍵字          標記成為新類型的名稱struct          inflatable{    std::string mystr;                   結構成員    char name[20];    float volume;    double price;};

 

c++ 在聲明結構變數的時候可以省略關鍵字struct

同時還要注意外部聲明, 和局部聲明

#include <iostream>#include <string>#include <cstring>struct inflatable{    std::string mystr;    char name[20];    float volume;    double price;};int main(int argc, char const *argv[]) {  using namespace std;  inflatable guest = {    "hello",    "Glorious Gloria",    1.88,    29.99  };  inflatable pal = {    "world",    "Audacious Arthur",    3.12,    32.99  };  int a=12.40;  std::cout << guest.mystr << ‘\n‘;  std::cout << a << ‘\n‘;  std::cout << "Expand your guest list with <<" << guest.name << ">>"            << "and" << "<<" << pal.name << ">>" << ‘\n‘;  std::cout << "you can have both for $" << guest.price + pal.price <<‘\n‘;  return 0;}
View Code

 

其他結構屬性
#include <iostream>#include <string>#include <cstring>struct inflatable{    std::string mystr;    char name[20];    float volume;    double price;};int main(int argc, char const *argv[]) {  using namespace std;  inflatable guest = {    "hello",    "Glorious Gloria",    1.88,    29.99  };  inflatable choice = guest;   這種方法叫成員賦值
或者

inflatable choice;
choice = guest;

  std::cout << "Expand your guest list with <<" << guest.name << ">>" << ‘\n‘;  std::cout << "choice choice.mystr ---->" << choice.mystr << ‘\n‘;  return 0;}

 

 

還可以

struct inflatable{    std::string mystr;    char name[20];    float volume;    double price;} mr_glitz = {"hello", "Glorious", 1.11, 11};
當然,也可以不賦值

 

結構數組

也可以建立元素為結構的數組, 方法和建立基本類型數組完全相同。例如:

#include <iostream>#include <string>#include <cstring>struct inflatable{    std::string mystr;    char name[20];    float volume;    double price;} mr_glitz = {"hello", "Glorious", 1.11, 11};int main(int argc, char const *argv[]) {  using namespace std;  inflatable guests[2] = {    {"hello", "doman", 2.1, 2.22},    // {"world", "corleone", 2.2, 3333}  };  std::cout << "guests[0].mystr: " << guests[0].mystr << ‘\n‘;  std::cout << "guests[1].name: " << guests[1].name << ‘\n‘;  return 0;}

 

 

 

結構中的位欄位

  

struct torgle_register{  unsigned int SN :  4;  unsigned int :4;  bool goodIN : 1;  bool goodTorgle : 1;}torgle_register tr = {14, true, false};

 

C++複合類型(結構體)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.