gpt4 book ai didi

C++ bad_alloc 在 vector intList 上使用 vecList.pop_back()

转载 作者:行者123 更新时间:2023-11-28 06:54:29 25 4
gpt4 key购买 nike

全部,完成学期并使用 vector 完成此学生作业。在执行 int remove() 函数期间出现间歇性 bad_alloc 错误和核心转储。我相信当我使用 intList.pop_back() 时会发生这种情况。这是我第一次用 vector 列表做任何事情,我很想知道为什么会这样,以及如何正确地做这件事以避免将来出现这个问题。任何想法将不胜感激。

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::endl;
using std::vector;

int remove(vector<int> *intList, int noOfElements, int removeItem);
void printResult(int removeItem, int removeReturn, int listLength);

int main()
{
// variable declarations
int removeReturn = 0;
int removeItem = 0;
unsigned int listLength = 0;
unsigned int counter = 0;
time_t seconds;
vector<int> intList;

// getting the time in seconds as a seed
time(&seconds);
// seeding the random number generator
srand((unsigned int) seconds);
// getting the random length for the calculations
listLength = rand() % (100 - 1 + 1) + 1;
std::cout << std::endl << "listLength is: " << listLength << std::endl;
// populate the array with numbers from 1 to listLength
std::cout << "Array:" << std::endl;
for(counter = 0; counter < listLength; counter++)
{
intList.push_back(rand() % (listLength - 1 + 1) + 1);
std::cout << intList.back() << " ";
if(counter > 0 && (counter + 1) % 10 == 0)
std::cout << std::endl;
}
std::cout << std::endl;
listLength = intList.size();
// generate a number in range to use for the search
removeItem = rand() % (listLength - 1 + 1) + 1;
// search for the number and delete it if found
// changing the size of the vector array as you do
std:: cout << "Searching for " << removeItem << " for removal." << std::endl;
removeReturn = remove(&intList, listLength, removeItem);
listLength = intList.size();
// output the results of the operation
printResult(removeItem, removeReturn, listLength);

return 0;
} // end main

int remove(std::vector<int> *intList, int noOfElements, int removeItem)
{
int counter;
int icounter;

// check to see if the vector list is empty
// if it is, exit with -1 (error message)
if(intList->empty())
return -1;
// assuming there is data in the vector list,
// loop through and look for the element
for(counter = 0; counter < noOfElements; counter++)
{
// if we do find an occurrence of the data in the vector list
if(intList->at(counter) == removeItem)
{
// overwrite it with the next piece of data
// and move the rest of the data up by one element
for(icounter = counter; icounter < noOfElements - 1; icounter++)
{
intList[icounter] = intList[icounter + 1];
}
// now that we have moved every data piece up one element
// delete the last element from the list
intList->pop_back();
// return the position where the data was found
return counter;
}
}
// return 0 because we didn't find the data
return 0;
} // end remove

void printResult(int removeItem, int removeReturn, int listLength)
{
if(removeReturn > 0)
{
std::cout << "Found " << removeItem << " at position " << removeReturn + << std::endl;
std::cout << "and deleted it from the list " << std::endl;
std::cout << "The new list length is " << listLength << std::endl;
}
else if(removeReturn == -1)
std::cout << "The list was empty" << std::endl;
else if(removeReturn == 0)
{
std::cout << "Value " << removeItem << " not found" << std::endl;
exit(101);
}
} // end printResult

最佳答案

如果您正在尝试做的是在 std::vector 中搜索一个项目并将其删除,那么这样做不需要复杂的函数:

#include <algorithm>
//...
intList.erase(std::remove(intList.begin(), intList.end(), removeItem), intList.end());

这一行完成了整个 remove 函数试图做的所有事情。

http://www.cplusplus.com/reference/algorithm/remove/ http://www.cplusplus.com/reference/vector/vector/erase/

一般来说,学习使用STL算法。在容器中移动、删除和排序项目是在程序中例行完成的操作,因此存在执行这些任务的算法。

关于C++ bad_alloc 在 vector <int> intList 上使用 vecList.pop_back(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23347034/

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