【问题标题】:How to solve the error 'std::out_of_range' what(): _M_range_check , vector will accept values until told to break如何解决错误 'std::out_of_range' what(): _M_range_check ,向量将接受值,直到被告知中断
【发布时间】:2015-12-04 13:22:53
【问题描述】:

您好,我正在尝试创建一个非常简单的向量程序来接收整数,然后在出现提示时,用 c++ 显示最大的。但是,我对此并不陌生,并且不可避免地对此感到困惑。如上所述,它会抛出错误 'std::out_of_range' what(): _M_range_check ,并且在提示中断之前接受值。但是我不知道为什么,即使浏览了这个论坛很长一段时间。感谢您提前提供任何帮助。

#include <iostream>
#include <vector>

using namespace std;

int inputVector (vector<int>);   //prototype functions
void sortVector    (int,vector<int>&files);
void displayVector (vector<int>&files);

int main()
{
    int vectorsize=(0);

    vector <int>files;

    files.reserve(10);

    vectorsize=inputVector (files);

    sortVector (vectorsize, files);

    displayVector (files);

    return 0;

}

void displayVector(vector<int>& files)
{

    cout << " The largest file size is " << files.at(0);

}

int  inputVector(vector<int>files)
{
    int file=0;
    int vectorsize;

    cout << "Enter file sizes  in Megabytes, file size has to be 1 or greater,                                                hit 0 to display max value " << endl;

    do
    {
        cin >> file;

        if (file==0)
        {
            break;
        }

        else
        {
            files.push_back(file) ; //sends value to the vector

            cout << files.size() << endl;
        }
    }
    while (file>0);

    vectorsize=files.size();
    return vectorsize;

}

void sortVector(int vectorsize,vector<int>&files)
{
    int maximum;

    for( int j=0;j<vectorsize;j++)
    {
        for(int i=0;i<(vectorsize-1);i++)
        {
            if(files.at(i)< files.at(i+1))
            {
                maximum=files.at(i);
                files.at(i)=files.at(i+1);
                files.at(i+1)=maximum;
            }
        }
    }
}

【问题讨论】:

  • 您是否至少看过问题预览?
  • 是的,因此提到了我浏览论坛。
  • 不,我的意思是在 gareththegeek 把它变成至少某种形状之前,你的问题中存在的可怕格式。

标签: c++ vector


【解决方案1】:

inputVector 按值获取向量,因此它正在修改副本。而是通过引用传递它。此外,您无需传递矢量大小,因为您只需调用 size()

【讨论】:

  • 我如何称呼尺寸?
  • @goon1345 files.size()
  • 干杯,现在没有给我匹配的函数调用错误,但我会看看我是否可以自己解决这个问题。
【解决方案2】:

您的vectorsize 错了,因此您的电话

files.at(i);  // in sortvector()

导致超出范围的错误。实际的向量大小是0,但你的程序假设了别的东西。这是由于将 vector 按值传递给函数 vectorinput() 造成的:在该函数返回时,原始向量没有改变,返回的 vectorsize 毫无意义。

做你想做的事

#include <iostream>
#include <vector>
#include <algorithm>

void inputVector (vector<int>&);

int main()
{
  vector<int> files;
  inputVector(files);
  // std::sort(files.begin(),files.end(),std::greater<int>()); // not needed
  std::cout << " The largest file size is "
            << std::max_element(files.begin(),files.end()) << std::endl;
}

void inputVector(vector<int>&files)
{
  std::cout << "Enter file sizes in Megabytes, file size has to be 1 or "
               "greater, enter 0 to display max value " << std::endl;
  files.reserve(10); // not really needed
  while(true) {
    int file;
    std::cin >> file;
    if (file>0) {
      files.push_back(file);
      // std::cout << files.size() << std::endl; // for debugging
    } else
      break;
  }
}

但是,当然,如果您只想找到最大值,则无需将文件大小存储在向量中。

【讨论】:

  • 感谢您和另一位评论员的建议,我现在已将其更改为通过引用传递。
  • 我知道文件保留并不是真正需要的,但我想,为什么不呢。非常感谢您为回答这个问题所做的努力,我很感激。
  • 它没有显示,因为声誉低,或者至少它告诉我什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多