我创建了两个向量,用push_back填充另一个向量,另一个用索引填充.我希望这些是平等的,但不是.有人可以解释一下这是为什么吗?
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v0;
v0.push_back(0);
v0.push_back(1);
v0.push_back(2);
vector<int> v1;
v1.reserve(3);
v1[0] = 0;
v1[1] = 1;
v1[2] = 2;
if (v0 != v1) {
cout << "why aren't they equal?" << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)