C++基础(五) 字符串、向量和数组

标准库类型String

读写string对象

1
2
3
string s;
cin >> s; //from empty to empty
getline(cin,s); //one line, stop by ENTER

当把string对象和字符(串)字面值混在一条语句时,必须确保加法运算符(+)两侧的运算对象至少一个是string

标准库类型vector

vector是模板而非类型

1
2
3
4
5
vector<T> v1;
vector<T> v2(v1);
vector<T> v3 = v1; //same to above
vector<T> v4{a,b,c,...};
vector<T> v5 = {a,b,c,...}; //same to above

凡是使用了迭代器的循环体,都不要向迭代器所属的容器添加元素

数组

理解复杂的数组声明(P102-103)

从变量名开始由内往外看,即变量名-数组-变量类型

1
2
3
4
int *ptrs[10]; //含有10个整型指针的数组
int (*parrary)[10] = &arr; //指向一个含有10个整数的数组的指针
int (&arrREF)[10] = arr; //引用一个含有10个整数的数组
int *(&arry)[10] = ptrs; //引用一个含有10个整型指针的数组

指向一个数组的指针+1后指向数组尾部的下一个地址

1
2
3
4
5
6
7
int arr[2];
int(*a)[2] = &arr;
int *b = arr;
cout << arr << endl; //0x61fe08
cout << arr + 1 << endl; //0x61fe0c
cout << a + 1 << endl; //0x61fe10 = 0x61fe08 + (4*2)D
cout << b + 1 << endl; //0x61fe0c
作者

Martin

发布于

2023-05-26

更新于

2023-05-26

许可协议

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.

You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.