Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::list和std::vector中的堆栈溢出异常_C++_List_Vector - Fatal编程技术网

C++ std::list和std::vector中的堆栈溢出异常

C++ std::list和std::vector中的堆栈溢出异常,c++,list,vector,C++,List,Vector,我正在制作一个测试程序来检查std::list、std::vector和array的存储时间。我想在每个容器中存储200万个随机整数。然而,当存储大约1550个整数时,std::list和std::vector会导致堆栈溢出异常。除了减少整数的数量,我怎样才能避免它呢 #include <list> #include <vector> #include <iostream> #include <iomanip> #include <strin

我正在制作一个测试程序来检查std::list、std::vector和array的存储时间。我想在每个容器中存储200万个随机整数。然而,当存储大约1550个整数时,std::list和std::vector会导致堆栈溢出异常。除了减少整数的数量,我怎样才能避免它呢

#include <list>
#include <vector>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

void insert(list<short>& l, const short& value);
void _insert(list<short>& l, list<short>::iterator it, const short& value);
void insert(vector<short>& v, const short& value);
void _insert(vector<short>& v, vector<short>::iterator it, const short& value);
void insert(short arr[], int& logicalSize, const int& physicalSize, const short& value);
void _insert(short arr[], int& logicalSize, const int& physicalSize, const short& value, int& cur);

int main() {
    clock_t start, end;
    srand(time(nullptr));

    const int SIZE = 2000000;
    const short RANGE = 10000;
    list<short> l;
    vector<short> v;
    short* arr = new short[SIZE];
    int logicalSize = 0;

    // list
    cout << "List storage time test...";
    start = clock();
    for (int i = 0; i < SIZE; i++) {
        insert(l, (short)(rand() % (2 * RANGE + 1) - RANGE));
    }
    end = clock();
    cout << "Time: " << difftime(end, start) << endl << endl;

    // vector
    cout << "Vector storage time test...";
    start = clock();
    for (int i = 0; i < SIZE; i++) {
        insert(v, (short)(rand() % (2 * RANGE + 1) - RANGE));
    }
    end = clock();
    cout << "Time: " << difftime(end, start) << endl << endl;

    // array
    start = clock();
    cout << "Array storage time test...";
    for (int i = 0; i < SIZE; i++) {
        try {
            insert(arr, logicalSize, SIZE, (short)(rand() % (2 * RANGE + 1) - RANGE));
        }
        catch (string s) {
            cout << s << endl;
            system("pause");
            exit(-1);
        }
    }
    end = clock();
    cout << "Time: " << difftime(end, start) << endl << endl;

    delete[] arr;
    system("pause");
    return 0;
}

void insert(list<short>& l, const short& value) {
    _insert(l, l.begin(), value);
}

void _insert(list<short>& l, list<short>::iterator it, const short& value) {
    if (it == l.end() || value < *it) {
        l.insert(it, value);
        return;
    }
    else {
        it++;
        return _insert(l, it, value);
    }
}

void insert(vector<short>& v, const short& value) {
    _insert(v, v.begin(), value);
}

void _insert(vector<short>& v, vector<short>::iterator it, const short& value) {
    if (it == v.end()) {
        v.push_back(value);
        return;
    } else if (value < *it) {
        v.insert(it, value);
        return;
    } else {
        it++;
        return _insert(v, it, value);
    }
}

void insert(short arr[], int& logicalSize, const int& physicalSize, const short& value) {
    int cur = 0;
    _insert(arr, logicalSize, physicalSize, value, cur);
}

void _insert(short arr[], int& logicalSize, const int& physicalSize, const short& value, int& cur) {
    if (cur >= physicalSize) throw string("No space for array.");

    if (cur == logicalSize) {
        arr[cur] = value;
        logicalSize++;
        return;
    } else if (value < arr[cur]) {
        for (int i = logicalSize - 1; i >= cur; i--) {
            arr[i + 1] = arr[i];
        }
        arr[cur] = value;
        logicalSize++;
        return;
    } else {
        return _insert(arr, logicalSize, physicalSize, value, ++cur);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
无效插入(列表和列表、常量和值);
void _insert(list&l,list::iterator it,const short&value);
无效插入(向量和v、常量和值);
void _insert(vector&v,vector::iterator it,const short&value);
无效插入(短arr[],int和logicalSize,常量int和physicalSize,常量short和value);
void _插入(短arr[]、int和logicalSize、const int和physicalSize、const short和value、int和cur);
int main(){
时钟开始、结束;
srand(时间(nullptr));
const int SIZE=2000000;
常数短程=10000;
清单l;
向量v;
短*arr=新短[尺寸];
int logicalSize=0;
//名单
库特
然而,当存储大约1550个整数时,std::list和std::vector会导致堆栈溢出异常

只需迭代编写

void insert(list<short>& l, const short& value) {
    list<short>::iterator it = l.begin();
    for (;it != l.end() && value < *it;it++);
    l.insert(it, value);
}
void插入(列表和列表、常量和值){
list::iterator it=l.begin();
对于(;it!=l.end()&&value<*it;it++);
l、 插入(信息、价值);
}
您可以对vector使用相同的想法…您甚至可以重用该函数:

void insert(list<short>& l, const short& value) {
    list<short>::iterator it = getIterator(l.begin(),l.end(),value);
    l.insert(it, value);
}

void insert(vector<short>& v, const short& value) {
    vector<short>::iterator it = getIterator(l.begin(),l.end(),value);
    v.insert(it, value);
}

template<class InputIterator>
void getIterator(InputIterator it,InputIterator end){
    while (it != end && value < *it) {
        it++;
    }
    return it;
}
void插入(列表和列表、常量和值){
list::iterator it=getIterator(l.begin(),l.end(),value);
l、 插入(信息、价值);
}
无效插入(向量和v、常量和值){
vector::iterator it=getIterator(l.begin(),l.end(),value);
v、 插入(信息、价值);
}
模板
void getIterator(InputIterator it,InputIterator end){
while(it!=end&&value<*it){
it++;
}
归还它;
}

为什么要递归编写所有内容?这不是Scheme;您没有尾部调用消除。如果不需要,请不要使用递归。您可以使用循环来完成所有这些操作。值得一读。您的
\u insert
函数有与标准库实现细节冲突的危险。此外,如果您想要一个有序的只需插入所有内容,然后对其进行一次排序。这样会快得多。