在索引 0 为空时访问向量
Accessing vector at index 0 when it's empty
为什么我在第一步已经 pop_back()
的情况下仍然可以从 vectors[0]
中获取值?
我猜现在我的 vectors
是空的并通过调用 empty()
来检查它
#include <iostream>
#include <vector>
struct Vector2 {
int x, y;
Vector2(int _x, int _y)
: x(_x)
, y(_y) {}
Vector2(const Vector2& vec)
: x(vec.x)
, y(vec.y) {
std::cout << "[Copy] Vector2{" << x << "," << y << "} copied!"
<< std::endl;
}
void Print() {
std::cout << "[Print] Vector2{" << x << "," << y << "}" << std::endl;
}
};
void PrintVectors(const std::vector<Vector2>& vectors) {
std::cout << "---" << std::endl;
for (auto& vector : vectors) {
std::cout << "Vector2{" << vector.x << "," << vector.y << "}"
<< std::endl;
}
}
int main() {
std::vector<Vector2> vectors;
vectors.push_back({21, 78});
PrintVectors(vectors);
// First
vectors.pop_back();
if (vectors.empty()) {
std::cout << "Empty condition!" << std::endl;
}
vectors[0].Print(); // ---> I thought I'd get an error at this point.
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
// Second
vectors.pop_back();
vectors[0].Print();
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
// Third + no pop_back needed
vectors[0].Print();
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
// 4th
vectors.pop_back();
vectors[0].Print();
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
}
然后,这个条件让我通过gdb调试。我看到 _M_
的值在变化,但我不知道发生了什么(我看不到 _M_start
和 _M_finish
之间的模式或关系)。
使用 operator[]
访问索引 0 处的空向量是越界的,因此 undefined behaviour 这样做会使整个程序无效。允许编译器生成它想要的任何结果。 任何行为都是可以接受的。
你不能推理包含 UB 的程序。
为什么我在第一步已经 pop_back()
的情况下仍然可以从 vectors[0]
中获取值?
我猜现在我的 vectors
是空的并通过调用 empty()
#include <iostream>
#include <vector>
struct Vector2 {
int x, y;
Vector2(int _x, int _y)
: x(_x)
, y(_y) {}
Vector2(const Vector2& vec)
: x(vec.x)
, y(vec.y) {
std::cout << "[Copy] Vector2{" << x << "," << y << "} copied!"
<< std::endl;
}
void Print() {
std::cout << "[Print] Vector2{" << x << "," << y << "}" << std::endl;
}
};
void PrintVectors(const std::vector<Vector2>& vectors) {
std::cout << "---" << std::endl;
for (auto& vector : vectors) {
std::cout << "Vector2{" << vector.x << "," << vector.y << "}"
<< std::endl;
}
}
int main() {
std::vector<Vector2> vectors;
vectors.push_back({21, 78});
PrintVectors(vectors);
// First
vectors.pop_back();
if (vectors.empty()) {
std::cout << "Empty condition!" << std::endl;
}
vectors[0].Print(); // ---> I thought I'd get an error at this point.
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
// Second
vectors.pop_back();
vectors[0].Print();
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
// Third + no pop_back needed
vectors[0].Print();
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
// 4th
vectors.pop_back();
vectors[0].Print();
vectors.insert(vectors.begin(), {vectors[0].x + 1, vectors[0].y});
PrintVectors(vectors);
}
然后,这个条件让我通过gdb调试。我看到 _M_
的值在变化,但我不知道发生了什么(我看不到 _M_start
和 _M_finish
之间的模式或关系)。
使用 operator[]
访问索引 0 处的空向量是越界的,因此 undefined behaviour 这样做会使整个程序无效。允许编译器生成它想要的任何结果。 任何行为都是可以接受的。
你不能推理包含 UB 的程序。