J'ai ce programme qui remplit des vecteurs avec des valeurs flottantes, en s'assurant que chaque vecteur (bin) a moins d'une somme de 1,0 lorsque les valeurs flottantes sont totalisées. J'ai découvert en déboguant que le programme s'arrête lorsqu'il essaie d'ajouter les valeurs d'un vecteur à un flottant. La raison en est que la boucle for est exécutée plus de fois que les éléments qui existent dans le vecteur. J'ai inséré un cout pour vérifier la valeur de la taille et j'obtiens un nombre très élevé (95 millions quelque chose).
//Bin packing algorithm
//Takes float values which are <1 and packs them into bins.
#include <iostream>
#include <vector>
#include <list>
#include <cassert>
using namespace std;
typedef unsigned int uint;
bool gt( float a, float b )
{
return b < a;
}
int main()
{
list< float > S;
while( ! cin.eof() )
{
float value;
if( cin >> value && ! cin.eof() )
{
assert( value > 0.0 && value <= 1.0 );
S.push_back( value );
}
}
uint n = S.size();
uint operations = 0;
vector< vector< float > > bins;
S.sort( gt );
float currentitem;
float binsum;
uint totalbins = 0;
uint currentbin;
vector<float> tempbin;
while(S.size() != 0)
{
currentitem = S.front();
S.pop_front();
currentbin = 0;
while(currentitem != 0 && totalbins > currentbin)
{
currentbin++;
bintotal = 0;
for(uint i=0; i < bins[currentbin].size(); i++)
{
cout << "i=" << i <<" bins[currentbin].size()=" << bins[currentbin].size() << endl;
binsum += bins[currentbin][i]; //THIS IS WHERE THE DEBUGGER POINTS. bins[currentbin].size() is producing the 95 million garbage value.
}
if((binsum + currentitem) <= 1)
{
currentitem = 0;
bins[currentbin].push_back(currentitem);
}
}
if(currentitem != 0)
{
totalbins++;
tempbin.push_back(currentitem);
bins.push_back(tempbin);
tempbin.clear();
}
}