其实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

相关文章: