C++ bad_alloc using vecList.pop_back() on vector <int> intList -


All, finish this semester and do this student assignment with vector. I am getting core dumps during execution of an intermittent bad_alok error and int extract () function. I believe when I use intList.pop_back (). This is my first time doing anything with a vector list and I would be interested in learning why this is happening and how to do it properly to avoid this problem in the future. Any thoughts would be greatly appreciated.

  #include & lt; Iostream & gt; # Include & lt; Cstdlib & gt; # Include & lt; Ctime & gt; Using std :: cout; Using the Std :: endl; Using Std :: vector; Intra removal (vector & lieutenant; int); * IntList, int noOfElements, int removeItem); Zero print result (int exit item, int extract, return, int list); Int main () {// variable declarations int removeReturn = 0; Int removeItem = 0; Unsigned int listLength = 0; Unsigned int counter = 0; Time_ seconds; Of vector & lt; Int & gt; IntList; // Twice time (and seconds) in the form of seconds; // Beijing random number generator (sec (unsigned int) seconds); // Getting the Random Length for the Count List length = rand ()% (100 - 1 + 1) + 1; Std :: cout & lt; & Lt; Std :: endl & lt; & Lt; "List lamp:" & lt; & Lt; List Lange & amp; Lt; & Lt; Std :: endl; // 1. Populate the array with numbers in the list length std :: cout & lt; & Lt; "Array:" & lt; & Lt; Std :: endl; (Counter = 0; counter & lt; listLength; counter ++) {intList.push_back (rand (%) (list lang - 1 + 1) + 1); Std :: cout & lt; & Lt; IntList.back () & lt; & Lt; ""; If (counter> 0 & amp; amp; amp; (counter + 1)% 10 == 0) std :: cout & lt; & Lt; Std :: endl; } Std :: cout & lt; & Lt; Std :: endl; ListLength = intList.size (); // To use search delete, generate a number in the range = rand ()% (listLength - 1 + 1) + 1; // Search for the number and delete it if you // change the shape of the vector array as if you were std :: cout & lt; & Lt; "Search for" & lt; & Lt; Removal item & lt; & Lt; "To remove." & Lt; & Lt; Std :: endl; RemoveReturn = Remove (& amp; intList, listLength, removeItem); ListLength = intList.size (); // Output print results output print (remove item, removal process, list length); Return 0; } // ending the main integer (std :: vector  * intList, int noOfElements, int removeItem) {int counter; Int iCard; // Check to see if the vector list is empty // if it is, then exit -1 (error message) if (intList-> empty ()) return -1; // is the figure in the vector list, // loop and for element (counter = 0; counter & lt; notifferences; counter ++) {// if we get an event of data in the vector list (IntList -> at (counter) == removeItem) {// Overwrite it with the second part of the next data / and the remaining data for an element (icounter = counter; icounter  Pop_back (); // Return the situation where the data returned counter counter; }} // Return 0 because we did not get data return 0; } // remove the end zero print protection (int removal unit, int refund, int list) {if (removeReturn> 0) {std :: cout & lt; & Lt; "Found" & lt; & Lt; Removal item & lt; & Lt; "At position" & lt; & Lt; Delete + & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "And removed it from the list" & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "New list length" & lt; & Lt; List Lange & amp; Lt; & Lt; Std :: endl; } And if (removeReturn == -1) std :: cout & lt; & Lt; "The list was empty" & lt; & Lt; Std :: endl; Else if (removeReturn == 0) {std :: cout & lt; & Lt; "Value" & lt; & Lt; Removal item & lt; & Lt; "Not Found" & lt; & Lt; Std :: endl; Getting Out (101); }} And print result  

If you are trying to do then < One item in Code> std :: vector and erase it, does not take a complex function to do this:

  #include & lt; Algorithm & gt; // ... intList.erase (std :: remove (intList.begin (), intList.end (), removeItem), intList.end ());  

She tries to do a line your entire delete function

In general, STL Learn how to use algorithms. The functions of transferring, deleting, and ordering items in the container are programs that are regularly performed in the program, so algorithms are present which do these tasks.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -