c++;未处理的异常 你好,我是C++的新用户,我有个问题 我想把一个文本文件读入vector。这个文件有57000行,我有这个问题 项目2.EXE中0x764 D4dBD中未处理的异常:微软C++异常:STD::BADYOLL在内存位置0x07CEB48…< /P>

c++;未处理的异常 你好,我是C++的新用户,我有个问题 我想把一个文本文件读入vector。这个文件有57000行,我有这个问题 项目2.EXE中0x764 D4dBD中未处理的异常:微软C++异常:STD::BADYOLL在内存位置0x07CEB48…< /P>,c++,c++11,C++,C++11,这是我的代码 #include "stdafx.h" #include <iostream> #include <iomanip> #include <fstream> #include <cmath> #include <vector> #include <string> #include <malloc.h> using namespace std; const int m = 17000; const i

这是我的代码

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <malloc.h>
using namespace std;

const int m = 17000;
const int n = 2;

std::vector< vector<long double> > student (m, vector<long double>(m,n));

ifstream inpStud("Carleton91.stu");
ofstream outData ("output.txt");

void readstudent(){

    long double k, numline;
        //assign all data to an array
    for (int i= 0; i<m; i++)
    {
        for (int j= 0; j<n; j++)
        {
            inpStud >> student[i][j];   
            k=student[i][j];    
        }

        //count number of exam
        if ((k==2) && (i>100)){
                numline = i;
                break;

        }
    }

    //print data
    for (int i= 0; i<numline; i++)
    {
        for (int j= 0; j<n; j++)
        {
            outData << student[i][j] << "    ";
        }
        outData << endl;
    }
}

int main() 
{

    readstudent();

    inpStud.close();
    outData.close();
    return 0;
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int m=17000;
常数int n=2;
std::vectorstudent(m,vector(m,n));
ifstream inpStud(“Carleton91.stu”);
流输出数据(“output.txt”);
void readstudent(){
长双k,核型线;
//将所有数据分配给一个数组
对于(int i=0;i学生[i][j];
k=学生[i][j];
}
//计算考试次数
如果((k==2)和&(i>100)){
numline=i;
打破
}
}
//打印数据
对于(int i=0;i
const int m=17000;
常数int n=2;
std::vectorstudent(m,vector(m,n));
这将创建一个包含17000个向量的
向量
,每个向量依次包含17000个
长双精度
s,每个向量初始化为2

long double
s通常每个至少有8个字节,具体取决于系统。现在让我们看一下下限。忽略
vector
中的所有开销,您正在分配空间来存储17000*17000=2.89亿
long double
值;假设每个
long double
有8个字节,即23.12亿字节,或大约2.15GB


Windows上32位程序的用户模式地址空间通常为2GB(32位指针只能寻址4GB,其中一半保留给操作系统使用),这意味着程序不可能分配所有内存。其中一个分配失败,抛出一个
std::bad_alloc
异常,导致出现错误。

假设您想为每个学生存储多个值,那么更好的选择是使用
std::map
。这样,您就有了一个单个学生映射到与该学生关联的所有数据的向量

由于数据似乎只是一个int,因此地图中的所有数据项都需要表示
向量

以下代码适用于您的文件,没有任何问题(如果文件URL链接断开,则文件由两行整数组成——第一行是学生编号,第二行是课程编号):

#包括
#包括
#包括
#包括
#包括
#包括
typedef std::map MapIntToVector;
使用名称空间std;
int main()
{
MapIntToVector m_mapIntVect;
ifstream ifs(“Carleton91.stu”);
int studentNumber,studentCourse;
while(ifs)
{
ifs>>学生编号>>学生课程;
m_mapIntVect[studentNumber]。向后推(studentCourse);
}
//输出数据
MapIntToVector::迭代器it=m_mapIntVect.begin();
while(it!=m_mapVect.end())
{
cout second.end(),ostream_迭代器(cout,“”);

无法学习使用调试器。在启用所有警告和调试信息的情况下编译。17000*17000的
长双精度
s向量?超过2GB。我的灵力告诉我,你将此编译为32位程序,因此你没有用户模式地址空间。你真的认为每个学生都有17000分吗?@s我必须坦率地说。你首先需要做的是减少编码。你需要的是更多地理解你试图解决的问题。退后一步,仔细思考你的问题所需的数据结构。你真的需要17000 x 17000数组来处理学生分数吗?现实生活中的人类教师会计算吗17000 x 17000电子表格上的标记?@shuthairah我看了你的文件。它有57000多行,每行2个数字。所以我不知道你是从哪里得到17000的。你需要的是一个
struct
,有2个数据成员,每行数字对应一个。然后创建一个
struct
s的
向量,而不是set设置初始大小并使用
vector::operator[]
,将每一行读入一个临时的
struct
实例,然后
vector::push_back
它。你真的应该开始阅读文档,熟悉你正在使用的类,然后再开始写代码。
const int m = 17000;
const int n = 2;

std::vector< vector<long double> > student (m, vector<long double>(m,n));
#include <vector>
#include <map>
#include <fstream>
#include <iterator>
#include <iostream>
#include <algorithm>

typedef std::map<int, std::vector<int>> MapIntToVector;

using namespace std;
int main()
{
    MapIntToVector m_mapIntVect;
    ifstream ifs("Carleton91.stu");
    int studentNumber, studentCourse;
    while (ifs)
    {
        ifs >> studentNumber >> studentCourse;
        m_mapIntVect[studentNumber].push_back(studentCourse);
    }
    // output data
    MapIntToVector::iterator it = m_mapIntVect.begin();
    while (it != m_mapVect.end())
    {
        cout << "Here is student " << it->first << " course(s) taken: ";
        std::copy(it->second.begin(), it->second.end(), ostream_iterator<int>(cout, " "));
        cout << "\n";
        ++it;
    }
}