C++ vector.push_back()上的另一个程序崩溃

C++ vector.push_back()上的另一个程序崩溃,c++,c++11,C++,C++11,我正在编写一个程序,从文本文件中读取数据,以各种方式对数据进行排序,然后将排序后的数据用于其他计算。让我说,在intmain(){}中有一个大for循环,它包含许多其他for循环,我发布的代码段只是主for循环中众多for循环中的一个 for(t=(compare.t - 1000)*2 - 12 ;t< ( (compare.t - 1000)*2 + 12 /);t++){ rank.push_back(vector <int>() );p++; fo

我正在编写一个程序,从文本文件中读取数据,以各种方式对数据进行排序,然后将排序后的数据用于其他计算。让我说,在
intmain(){}
中有一个大for循环,它包含许多其他for循环,我发布的代码段只是主for循环中众多for循环中的一个

 for(t=(compare.t - 1000)*2 - 12 ;t< ( (compare.t - 1000)*2 + 12 /);t++){

  rank.push_back(vector <int>() );p++;


     for(ii=111;ii<2489;ii++){
          if(M_screened[a][ii].size()>0){

             if( fabs(M_screened[a][ii][0].t-(t*0.5 + 1000.0)) <0.257 ){
                  cout<<"ii = "<<ii<<" t = "<<t<<"  sizeof(rank) = "<<rank.size()<<"  p = "<<p;
                  cout<<"   rank["<<p<<"].size() = "<<rank[p].size()<<endl;
                  cout<<"   "<<endl;
                  cout<<"address = "<<&rank[p][rank[p].size()-1];
                  rank[p].push_back(ii);
                  cout<<" "<<endl;
              }
          }
      }       
 foutT<<t*0.5 + 1000<<" "<<frequency[a][t]<<endl;} //end t-loop
以下是我从Microsoft Visual Studio收到的错误消息:

Unhandled exception at 0x0000000077993290 (ntdll.dll) in Muon_fitting7_7_2014_statistics.exe: 0xC0000005: Access violation reading location 0x0000038085FFA1E8.

如果我能提供任何其他信息,请让我知道

我假设您在声明时已将p初始化为零或类似的东西,这实际上是您的问题

for(t=(compare.t - 1000)*2 - 12 ;t< ( (compare.t - 1000)*2 + 12 /);t++)
{
    rank.push_back(vector <int>() );p++;
    for(ii=111;ii<2489;ii++)
    {
        if(M_screened[a][ii].size()>0){
            if( fabs(M_screened[a][ii][0].t-(t*0.5 + 1000.0)) <0.257 ){
              rank[p].push_back(ii);
            }
        }
    }   
}
for(t=(compare.t-1000)*2-12;t<((compare.t-1000)*2+12/);t++)
{
rank.push_back(向量());p++;
对于(ii=111;ii0){

如果(晶圆厂(M_-screen[a][ii][0].t-(t*0.5+1000.0))嘿,Dan,我已经按照你的建议做了,在其他情况下效果很好-我有相同的代码在稍微不同的版本中执行。这个版本的区别是,包含我前面提到的所有内容的for循环很大,在其他版本中,它是一个while循环,保存所有内容。此外,我在排名[p]后的第二名。向后推(ii)语句来验证它是否正好在那里崩溃。嘿,Dan,在我提供的参数列表中,您可以看到索引是正确的。此外,这是使用变量t的for循环的第177次迭代,并且以相同的方式创建了许多其他秩向量。我很高兴将代码简化为一个非常基本的examp如果你想看的话。
for(t=(compare.t - 1000)*2 - 12 ;t< ( (compare.t - 1000)*2 + 12 /);t++)
{
    rank.push_back(vector <int>() );p++;
    for(ii=111;ii<2489;ii++)
    {
        if(M_screened[a][ii].size()>0){
            if( fabs(M_screened[a][ii][0].t-(t*0.5 + 1000.0)) <0.257 ){
              rank[p].push_back(ii);
            }
        }
    }   
}
int p = -1;
for(t = ((compare.t - 1000)*2 - 12);t < ((compare.t - 1000)*2 + 12);++t)
{
    rank.push_back(vector <int>() );
    ++p;
    for(ii=111;ii<2489;++ii)
    {
        if (M_screened[a][ii].size() > 0)
        {
            if (fabs(M_screened[a][ii][0].t-(t*0.5 + 1000.0)) < 0.257)
            {
                rank[p].push_back(ii);
            }
        }
    }   
}