当使用大于 3000 万的数据输入大小时,程序不会将数据输出到控制台

Program will not output data to console when using a data input size greater than 30 million

我正在尝试制作一个程序,该程序最终将通过使用二叉搜索树和向量来显示大数据输入的运行时差异。但在此之前,我正在测试插入和搜索功能是否正常工作。好像没问题,但是每当我指定 SIZE 为 3000 万或更多时,大约 10-20 秒后,它只会显示 Press any key to continue... 而没有输出。但是,如果我将 SIZE 指定为等于或小于 2000 万,它将按照我编程的方式输出搜索结果。那么您认为是什么导致了这个问题?

一些旁注:

我将随机生成的唯一(无重复)值存储到树和向量中。所以最后,树和向量都将具有完全相同的值。当程序运行搜索部分时,如果在 BST 中找到了一个值,那么它也应该在向量中找到。到目前为止,当使用 2000 万个或更少的值时,这没有问题。

此外,我使用 randValue = rand() * rand(); 来生成随机值,因为我知道 rand() 的最大值是 32767。因此将它自身相乘将保证数字范围为 0 - 1,073,741,824。我知道我使用的插入和搜索方法效率低下,因为我要确保没有重复项,但这不是我现在关心的问题。这只是为了我自己的练习。

为了简单起见,我只是 post整理我的 main.cpp。如果您认为问题出在我的其他文件之一,我会 post 剩下的。

这是我的 main.cpp:

#include <iostream>
#include <time.h>
#include <vector>
#include "BSTTemplate.h"
#include "functions.h"

using namespace std;

int main()
{
    const long long SIZE = 30000000;
    vector<long long> vector1(SIZE);
    long long randNum;
    binarySearchTree<long long> bst1;
    srand(time(NULL));

    //inserts data into BST and into the vector AND makes sure there are no duplicates
    for(long long i = 0; i < SIZE; i++)
    {
        randNum = randLLNum();
        bst1.insert(randNum);
        if(bst1.numDups == 1)//if the random number generated is duplicated, don't count it and redo that iteration
        {
            i--;
            bst1.numDups = 0;
            continue;
        }
        vector1[i] = randNum;
    }

    //search for a random value in both the BST and the vector
    for(int i = 0; i < 5; i++)
    {
        randNum = randLLNum();
        cout << endl << "The random number chosen is: " << randNum << endl << endl;

        //searching with BST
        cout << "Searching for " << randNum << " in BST..." << endl;
        if(bst1.search(randNum))
            cout << randNum << " = found" << endl;
        else
            cout << randNum << " = not found" << endl;

        //searching with linear search using vectors
        cout << endl << "Searching for " << randNum << " in vector..." << endl;
        if(containsInVector(vector1, SIZE, randNum))
            cout << randNum << " = found" << endl;
        else
            cout << randNum << " = not found" << endl;
    }

    cout << endl;

    return 0;
}

(应 OP 的要求将评论重新发布为答案)

选项包括:编译 64 位(如果您还没有 - 可能会更好或更差,具体取决于 RAM 或地址 space 是问题),购买更多内存,调整操作系统的交换内存设置(让它使用更多的磁盘),设计一个内存效率更高的树(但最多你可能只会得到一个数量级的改进,也许更少,并且它可能会影响性能特征等其他东西),重新设计你的树所以它手动将数据保存到磁盘并读回(例如使用 LRU)。

以下是在 VC++ 上编译 64 位的方法:msdn.microsoft.com/en-us/library/9yb4317s.aspx