变量

C++的变量

  • 变量的初始化
    int x=0;
    int x (0);
    int x {0};
    圆括号()和花括号{}都可以用来赋值。方括号[]一般是数组里用的
  • 自动数据类型
    autodecltype
    使用auto可以自动确定数据类型,如下
  int foo = 0;
  auto bar = foo;  // the same as: int bar = foo;

  首先明确的了foo的类型是int,然后使用auto来给bar自动分配类型,也是int

  使用decltype也可以为变量定义数据类型

  int foo = 0;
  decltype(foo) bar ;  // the same as: int bar;

  注意这两种定义情况下的变量位置顺序。

  • string 数据类型
      可以直接给变量赋值一个字符串,而不用像C语言一样给数组的每个元素赋值,省事。.

      输出结果就是This is a string,很方便。
      和上面一样,初始化string对象也可以用(){},如下图。

      关于string 类型的详细使用以后在写,先知道这个类型。   

posted on 2019-05-16 14:10  chungehpu  阅读(156)  评论(0编辑  收藏  举报

导航