i have the next code that compiles without problems but when I run it after I put in the input data it gives me the next error:
line 128 vector iterator not decrementable
I use visual c++ 2010.
#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>
/* Get the number of different lengths from the user */
struct Cut{
unsigned number;
double length;
};
int main () {
std::vector< Cut > allCuts;
Cut currentCut;
unsigned numberOfSizes;
std::cout << "Enter number of different sizes: ";
std::cin >> numberOfSizes;
/* Now get the actual numbers and lengths from the user */
for(unsigned i = 0; i < numberOfSizes; ++i){
//currentCut = new Cut;
std::cout << "Enter new cut: <number> <length>" << std::endl;
std::cin >> currentCut.number >> currentCut.length;
/* Check that the length is sensible */
if((currentCut.length <= 0) || (currentCut.length > 12)){
std::cerr << "Error length must be in the range (0,12]" << std::endl;
--i;
}
else
allCuts.push_back(currentCut);
}
/* Now put all the cuts into a single large vector */
std::vector< double > lengths;
for(unsigned i = 0; i < allCuts.size(); ++i){
for(unsigned j = 0; j < allCuts[i].number; ++j)
lengths.push_back(allCuts[i].length);
}
/* Sort the vector */
std::sort(lengths.begin(), lengths.end());
/* Make a place to put the lengths to cut from each bar */
const unsigned NEW_BAR_LENGTH = 12;
std::vector< std::vector< double > >bars;
std::vector< double > currentBar;
double remainingLength = NEW_BAR_LENGTH;
/* Now keep going through the lengths vector until all the cuts are accounted for */
while(lengths.size() > 0){
/* start at the longest length that needs to be cut ... */
std::vector< double >::iterator it = lengths.end() - 1;
/* Go until the smallest length that's needed is reached */
while(it >= lengths.begin()){
/* Check if the current length will fit */
if(*it < remainingLength){ /* If it will... */
/* ... Add it to the current bar and remove it from the list to be fitted */
currentBar.push_back(*it);
/* Update the amount of the bar remaining */
remainingLength -= *it;
/* Remove the current element from the lengths vector */
lengths.erase(it);
}
/* Check that we didn't just remove the last element */
if(lengths.size() > 0){
/* length[0] is the shortest length cut that we still need to get
if this is more than the remaining amount of the current bar,
then we should finish this bar and start a new one, check for
this now */
if(lengths[0] > remainingLength){
/* Reset the remaining length */
remainingLength = NEW_BAR_LENGTH;
/* Add this completed bar to the vector of bars */
bars.push_back(currentBar);
/* Clear the current bar */
currentBar.clear();
/* Exit this loop and start on the new bar */
break;
}
}
--it;
}
}
double totalWaste = 0.0;
for(unsigned i = 0; i < bars.size(); ++i){
std::cout << "Bar " << std::setw(2) << i + 1 << ":\t";
for(unsigned j = 0; j < bars[i].size(); ++j)
std::cout << std::setprecision(3) << std::fixed << bars[i][j] << " ";
std::cout << std::endl;
std::partial_sum(bars[i].begin(), bars[i].end(), bars[i].begin());
totalWaste += NEW_BAR_LENGTH - bars[i].back();
}
/* Print a summary */
std::cout << "===============================" << std::endl;
std::cout << "Bars used:\t" << bars.size() << std::endl;
std::cout << "Total waste:\t" << totalWaste << " m" << std::endl;
std::cout << "Waste/Bar:\t" << totalWaste/bars.size() << " m" << std::endl;
std::cout << "===============================" << std::endl;
system("pause");
return 0;
}
So, if anyone could help me I would really appreciate it.
Thank you