gpt4 book ai didi

c++ - 仅剩余条目的 vector pop_back() 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:20 26 4
gpt4 key购买 nike

代码:

#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>

struct myClass {
bool bIsDead;
myClass(bool bDead) {
this->bIsDead = bDead;
}
};

void PrintVector(std::vector<myClass*> vec) {
std::cout << "The vector contains: ";
for(int i = 0; i < vec.size(); i++) {
std::cout << vec[i]->bIsDead << " ";
}
std::cout << std::endl;
}

int main() {

std::srand(std::time(0)); // use current time as seed for rng

std::vector<myClass*> myVector;
for(int i = 0; i < 10; i++) {
int tempRand = std::rand() % 2;
// int tempRand = 1;
if(tempRand == 1) {
myVector.push_back(new myClass(true));
}
else {
myVector.push_back(new myClass(false));
}
}

std::cout << "Unsorted: " << std::endl;
PrintVector(myVector);

std::sort(myVector.begin(), myVector.end(), [ ]( const myClass *lhs, const myClass *rhs )
{
return lhs->bIsDead < rhs->bIsDead;
});

std::cout << "Sorted: " << std::endl;
PrintVector(myVector);


while(myVector.back()->bIsDead) {
delete myVector.back();
myVector.pop_back();
}

std::cout << "Removed Dead Ones: " << std::endl;
PrintVector(myVector);

return 0;
}

随机输入的输出:

Unsorted: 
The vector contains: 0 0 1 0 0 0 1 1 0 1
Sorted:
The vector contains: 0 0 0 0 0 0 1 1 1 1
Removed Dead Ones:
The vector contains: 0 0 0 0 0 0

Hit ENTER to continue...

全 1 的输出(即 pop_back 删除所有):

Unsorted: 
The vector contains: 1 1 1 1 1 1 1 1 1 1
Sorted:
The vector contains: 1 1 1 1 1 1 1 1 1 1

Hit ENTER to continue...

对于这个控制台应用程序,我没有收到任何错误或崩溃警告,但它不会在 pop_back 调用全 1 的情况后输出两个 cout 语句。

关于为什么将指针 vector 弹出回空会导致程序崩溃的任何想法?

最佳答案

您需要添加对 vector 大小的检查。如果您想使用 back() 访问 vector 的最后一个元素,则它不应为 0。

改变这个:

while(myVector.back()->bIsDead) {

到:

while(myVector.size() > 0 && myVector.back()->bIsDead) {

正确代码如下:

#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>

struct myClass {
bool bIsDead;
myClass(bool bDead) {
this->bIsDead = bDead;
}
};

void PrintVector(std::vector<myClass*> vec) {
std::cout << "The vector contains: ";
for(int i = 0; i < vec.size(); i++) {
std::cout << vec[i]->bIsDead << " ";
}
std::cout << std::endl;
}

int main() {

std::srand(std::time(0)); // use current time as seed for rng

std::vector<myClass*> myVector;
for(int i = 0; i < 10; i++) {
int tempRand = std::rand() % 2;
// int tempRand = 1;
if(tempRand == 1) {
myVector.push_back(new myClass(true));
}
else {
myVector.push_back(new myClass(false));
}
}

std::cout << "Unsorted: " << std::endl;
PrintVector(myVector);

std::sort(myVector.begin(), myVector.end(), [ ]( const myClass *lhs, const myClass *rhs )
{
return lhs->bIsDead < rhs->bIsDead;
});

std::cout << "Sorted: " << std::endl;
PrintVector(myVector);


while(myVector.size() > 0 && myVector.back()->bIsDead) {
delete myVector.back();
myVector.pop_back();
}

std::cout << "Removed Dead Ones: " << std::endl;
PrintVector(myVector);

return 0;
}

引用 cppreference

Calling this function(back()) on an empty container causes undefined behavior.

PS:你不需要让问题看起来很吓人来引起我们的注意。

关于c++ - 仅剩余条目的 vector pop_back() 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com