简体   繁体   English

填充向量C ++时发生溢出错误

[英]Overflow error when filling vector C++

I need to read 3 text files filled with integers, perform 3 different sort methods (insertion, shell, and quick sort) on it, then put the sorted list into a new file. 我需要读取3个填充有整数的文本文件,对其执行3种不同的排序方法(插入,shell和快速排序),然后将排序后的列表放入一个新文件中。

My method for doing this was going to be storing the integers in 3 vectors, performing one type of sort on each vector, then writing the vectors to new files. 我这样做的方法是将整数存储在3个向量中,对每个向量执行一种类型的排序,然后将向量写入新文件。 I'm running into a stack overflow error on the second file while my program reads it though, and am starting to reconsider this method. 我的程序在读取第二个文件时遇到了堆栈溢出错误,并且开始重新考虑此方法。 My program gets through the first file just fine (it's about 10 lines), but the second file is 66 KB, and the third one is larger still (1,038 KB). 我的程序通过第一个文件就可以了(大约10行),但是第二个文件是66 KB,第三个文件还更大(1,038 KB)。

Is there a better way of going about this or is there some way to fix the overflow error? 是否有解决此问题的更好方法,还是有某种方法可以解决溢出错误?

Here is the problem code: 这是问题代码:

/* read through second file and add to vectors */
file2.open("data2.txt");
file2 >> data;
while (!file2.eof())
{
    v1.push_back(data);
    v2.push_back(data);
    v3.push_back(data);
    file2 >> data;
}
file2.close();

Here is my complete code so far: (I have not quite figured out writing to files yet so that part is commented out) 到目前为止,这是我完整的代码:(我还不太清楚写文件的方式,因此该部分已被注释掉)

#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <iostream>
#include <algorithm>
#include <utility>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>

/* member function declarations */
using namespace std;
void insertion_sort(vector<int> & v, int length);
void shellsort(vector<int> & v, int n);
int quickSort(vector<int> &, int start, int end);
int partition(vector<int> &, int start, int end, int& swaps);
void swap(int &val1, int &val2);

int main()
{
    /* file stream and clock declarations */
    ifstream file1, file2, file3;
    ofstream I1, I2, I3, S1, S2, S3, Q1, Q2, Q3;

    /* read through first file and add to vectors */
    file1.open("data1.txt");
    vector<int> v1, v2, v3;
    int data;
    file1 >> data;
    while (!file1.eof())
    {
        v1.push_back(data);
        v2.push_back(data);
        v3.push_back(data);
        file1 >> data;
    }
    file1.close(); // end file reading

    /* sort each vector using different methods */
    insertion_sort(v1, v1.size());
    shellsort(v2, v2.size());
    quickSort(v3, 0, v3.size() - 1);

    // write vectors to new files

    /* read through second file and add to vectors */
    file2.open("data2.txt");
    file2 >> data;
    while (!file2.eof())
    {
        v1.push_back(data);
        v2.push_back(data);
        v3.push_back(data);
        file2 >> data;
    }
    file2.close();

    /* sort each vector using different methods */
    insertion_sort(v1, v1.size());
    shellsort(v2, v2.size());
    quickSort(v3, 0, v3.size() - 1);

    // write to file

    /* read through third file and add to vectors */
    file3.open("data3.txt");
    file3 >> data;
    while (!file3.eof())
    {
        v1.push_back(data);
        v2.push_back(data);
        v3.push_back(data);
        file3 >> data;
    }
    file3.close();

    /* sort each vector using different methods */
    insertion_sort(v1, v1.size());
    shellsort(v2, v2.size());
    quickSort(v3, 0, v3.size() - 1);

    // write to file

    cout << endl;
    system("PAUSE");
    return 0;
}

/*insertion sort*/
void insertion_sort(vector<int> &v, int n) {
    int x, y, temp;
    for (x = 1; x < n; x++) {
    y = x;
    while (y > 0 && v[y - 1] > v[y]) {
        temp = v[y];
        v[y] = v[y - 1];
        v[y - 1] = temp;
        y--;
    }//end of while loop
}//end of for loop
}//end of insertion_sort.

 /* shellsort */
void shellsort(vector<int> & v, int n)

{
int k, x, y, temp;
for (k = n / 2; k > 0; k /= 2)
    for (x = k; x < n; x++)
        for (y = x - k; y >= 0 && v[y]>v[y + k]; y -= k) 
        {
            temp = v[y];
            v[y] = v[y + k];
            v[y + k] = temp;
        }
}

/* quicksort function */
int quicksort(vector<int> & v, int s, int last)
{
int swaps = 0;
if (s < last)
{
    int i = partition(v, s, last, swaps);
    swaps += quicksort(v, s, i - 1);
    swaps += quicksort(v, i + 1, last);
}
return swaps;
}

/* partition function for quicksorting */
int partition(vector<int> & v, int start, int end, int& swaps)
{
int pivot = v[end];
int index= start;

for (int i = start; i < end; i++)
{
    if (v[i] <= pivot)
    {

        if (index != i)
        {
            std::swap(v[i], v[index]);
            swaps++;
        }
        index++;
    }
}
if (index != end)
{
    std::swap(v[index], v[end]);
    swaps++;
}
return index;
}

OK, so I assume this is homework; 好的,所以我认为这是家庭作业。 so, here is an attempt to lead you to the answer without just giving it to you. 因此,这是尝试将您引向答案而不只是将其提供给您的尝试。

Note that the stack is used to hold local variables. 请注意,堆栈用于保存局部变量。 As a new function is called, additional variables (stack space) has to be allocated. 调用新函数时,必须分配其他变量(堆栈空间)。 If a function calls itself (called recursion) that can cause the stack to grow very quickly (and cause a stack overflow). 如果函数调用自身(称为递归),则可能导致堆栈快速增长(并导致堆栈溢出)。

Look for any functions that are calling themselves. 查找正在调用自己的任何函数。 How deep the nesting (calls within calls) can get? 嵌套(调用内的调用)可以达到多深? Your debug info could help you with that, or maybe just think about how the code works. 您的调试信息可以为您提供帮助,或者只是考虑代码的工作方式。 Also look at how many variables your function needs (that will need to be allocated on the stack). 还要查看您的函数需要多少个变量(这些变量需要在堆栈上分配)。 Multiply these two (call depth and local memory) and you will an idea of how much stack you will need. 将这两个乘积(调用深度和本地内存)相乘,您将了解需要多少堆栈。

Since you say the problem increases with file size, look at how file size affects the code. 既然您说问题随着文件大小而增加,请查看文件大小如何影响代码。 Does it affect the call depth or the amount of memory used? 它会影响通话深度或使用的内存量吗?

Once you know the cause, you can move onto a solution. 一旦知道了原因,就可以采取解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM