跳转至

类型和结构体

类型

auto 类型也是"静态"的

例如:

auto a = 0;

a = "hello" # 这样不可以

a = 5; 

size_t 用在索引更好

size_t 为非负数,实际大小取决于编译器和操作系统,通常 64位操作系统的大小为 8字节.

size_t 可以避免潜在的赋值问题

在某种情况下看起来像造成了一些问题(?)

例如在使用vector容器时:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> empty_vec = {};

    // vec.size() == 0
    for (int i = empty_vec.size() - 1; i >= 0; --i) {
        std::cout << "This should not print!\n";
    }

    return 0;
}

vec.size() 的返回值类型时size_t,运算的时候等式右边被隐式转换为size_t

using 可以创建类型的变量

已知:

std::pair<double,double> a;

using PA = std::pair<double,double>;

PA a;

此处上下两个式子等价.

结构体

特点:

  • 将多个变量集合在一个变量中
struct IdCard{
    string name;
    string id;
};



IdCard InitInfo(string name,string id){
    IdCard a;
    a.name = name;
    a.id = id;

    return a;
}