小编Ach*_*ara的帖子

C++ std :: bad_alloc错误

我正在研究一个C++程序(C++ 98).它读取包含许多行(10000行)的文本文件.这些是制表符分隔值,然后我将其解析为Vector of Vector对象.但它似乎适用于某些文件(较小)但我的一个文件给我以下错误(此文件有10000行,它是90MB).我猜这是一个与记忆有关的问题?你能帮我么?

错误

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Abort
Run Code Online (Sandbox Code Playgroud)

void AppManager::go(string customerFile) {

    vector<vector<string> > vals = fileReader(customerFile);

    for (unsigned int i = 0; i < vals.size();i++){

        cout << "New One\n\n";

        for (unsigned int j = 0; j < vals[i].size(); j++){

            cout << vals[i][j] << endl;
        }

        cout << "End New One\n\n";
    }
}

vector<vector<string> > AppManager::fileReader(string fileName) {

    string line;
    vector<vector<string> > values;

    ifstream inputFile(fileName.c_str());

    if (inputFile.is_open()) {

        while (getline(inputFile,line)) { …
Run Code Online (Sandbox Code Playgroud)

c++ memory memory-management vector

5
推荐指数
1
解决办法
2万
查看次数

如何在PuTTY上查看文件?

我在PuTTY中打开了一个文件,我需要文件.flag,如何打开它?

我试过了

vi index.flag
Run Code Online (Sandbox Code Playgroud)

cp index.flag
Run Code Online (Sandbox Code Playgroud)

ssh putty

2
推荐指数
1
解决办法
6万
查看次数

C++类型检查错误

我按照上一篇文章中的说明重新编写了代码.

我的头文件

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <typeinfo>
#include "Tour.h"
#include "GuidedTour.h"

using namespace std;
class TourManager {

private:
    vector<Tour *> tours;
    void setupTour();
    string getUserInput();
    string displayMainMenu();
    void displayTourDetails();
    void callDisplayOnEach();
    void addBookingsToTour();

public:
    TourManager();
    void go();
};
Run Code Online (Sandbox Code Playgroud)

然后我有一个函数用tour和guidedTour对象填充"list"向量.

void TourManager::setupTour() {

    tours.push_back(new Tour("FP001", "Fun Park 3 Day Pass", 110.00));
    tours.push_back(new GuidedTour("SK003", "Learn to Ski Adventure Tour", 240.00, "28/07
}

void TourManager::callDisplayOnEach() {

    for (vector<Tour *>::iterator it = tours.begin() ; it != tours.end(); ++it) 
    { …
Run Code Online (Sandbox Code Playgroud)

c++ types c++98

1
推荐指数
1
解决办法
199
查看次数

标签 统计

c++ ×2

c++98 ×1

memory ×1

memory-management ×1

putty ×1

ssh ×1

types ×1

vector ×1