放三个人之后 size 是多少
运行下面这段程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct SeatRow {
string* data; // 一排定长的座位(new[] 出来的)
int cap; // 一共几个座位
int size = 0; // 实际坐了几个
SeatRow(int c = 6) : data(new string[c]), cap(c) {}
~SeatRow() { delete[] data; }
void append(const string& x) {
if (size == cap) return; // 坐满了就放不进去(先不扩容)
data[size] = x;
size++;
}
};
int main() {
SeatRow r(6);
for (const string& x : {"阿岚", "小满", "阿泰"}) r.append(x);
cout << r.size << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论