我正在制作一个测试程序来检查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);
}
}
答案 0 :(得分: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);
}
你可以对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;
}