Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Vector.size()生成意外大的数字_C++_Vector - Fatal编程技术网

C++ Vector.size()生成意外大的数字

C++ Vector.size()生成意外大的数字,c++,vector,C++,Vector,我有一个用浮点值填充向量的程序,当浮点值相加时,确保每个向量(bin)的和小于1.0。我通过调试发现,当程序试图将向量值添加到浮点时,程序正在停止。原因是for循环的运行次数比向量中存在的元素要多。我插入了一个cout来检查大小的值,得到了一个非常大的数字(9500万左右) //装箱算法 //取浮点值,其为S; 而(!cin.eof()) { 浮动值; 如果(cin>>值&&!cin.eof()) { 断言(值>0.0&&value>bin; S.sort(gt); 浮动项目; 浮箱; uint

我有一个用浮点值填充向量的程序,当浮点值相加时,确保每个向量(bin)的和小于1.0。我通过调试发现,当程序试图将向量值添加到浮点时,程序正在停止。原因是for循环的运行次数比向量中存在的元素要多。我插入了一个cout来检查大小的值,得到了一个非常大的数字(9500万左右)

//装箱算法
//取浮点值,其为S;
而(!cin.eof())
{
浮动值;
如果(cin>>值&&!cin.eof())
{
断言(值>0.0&&value>bin;
S.sort(gt);
浮动项目;
浮箱;
uint totalbins=0;
uint-currentbin;
向量tempbin;
而(S.size()!=0)
{
currentitem=S.front();
美国波普阵线();
currentbin=0;
而(currentitem!=0&&totalbins>currentbin)
{
currentbin++;
bintotal=0;
对于(uint i=0;icout
bin[currentbin]
for
循环中的
for
访问无效的bin,因为
currentbin
变量已经增加。您需要在
for
循环之后使用
currentbin++

在使用
bin[currentbin]之前,我看不到任何地方都使用
bin
。size()
。另外,我会将您的循环更改为
while(std::cin>>值)
。这是最小值吗?我们可以输入什么值来重现错误?为什么不在
while
循环的末尾实际使用,因为它也用于
for
后面的
if
//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();
      }
   }