关于清空vector的4种方式
  • 板块学术版
  • 楼主封禁用户
  • 当前回复14
  • 已保存回复14
  • 发布时间2022/8/11 11:40
  • 上次更新2023/10/27 15:58:58
查看原帖
关于清空vector的4种方式
461366
封禁用户楼主2022/8/11 11:40

这是我的代码:

#include <bits/stdc++.h>
using namespace std;

void p(auto v) {
	cout << "vector = ";
	for (int x: v) cout << x << " ";
	cout << endl;
}

void a() {
	vector<int> v = {1, 2, 3};
	v.clear();
	p(v);
}

void b() {
	vector<int> v = {1, 2, 3};
	v = {};
	p(v);
}

void c() {
	vector<int> v = {1, 2, 3};
	// 这样写会报错:
	// v.swap(vector<int>());
	{
		vector<int> tmp;
		v.swap(tmp);
	}
	p(v);
}

int main() {
	a();
	b();
	c();
	return 0;
}

求问:

1.为什么注释中的代码报错?

2.b函数的写法正确吗?

3.三种方法的时间复杂度。

2022/8/11 11:40
加载中...