我在推回函数中编写了一个容量函数,以返回当前为向量数组分配的存储空间的大小。如下图所示,是在插入测试用例期间,它无法正常工作(请看容量)。插件工作正常,但容量无法正常工作,并以某种方式导致中止的核心在其末尾转储。有人可以向我强调为什么吗?谢谢!
错误的输出:
********** TestInsert **********
(size=0, capacity=0)
push_back 8 floats:
0 (size=1, capacity=1)
0 1.41 (size=2, capacity=2)
0 1.41 2.82 (size=3, capacity=4)
0 1.41 2.82 4.24 (size=4, capacity=4)
0 1.41 2.82 4.24 5.65 (size=5, capacity=8)
0 1.41 2.82 4.24 5.65 7.06 (size=6, capacity=8)
0 1.41 2.82 4.24 5.65 7.06 8.47 (size=7, capacity=8)
0 1.41 2.82 4.24 5.65 7.06 8.47 9.89 (size=8, capacity=8)
insert(3, 99):
0 1.41 2.82 99 4.24 5.65 7.06 8.47 9.89 (size=9,
capacity=8)
insert(0, 98):
98 0 1.41 2.82 99 4.24 5.65 7.06 8.47 9.89
(size=10, capacity=8)
insert(6, 97):
98 0 1.41 2.82 99 4.24 97 5.65 7.06 8.47 9.89 (size=11, capacity=8)
Construct from int array:
6 6 16 14 -6 12 6 10 8 6 6 4 2 0 (size=14, capacity=1)
Aborted (core dumped)
正确的输出:
********** TestInsert **********
(size=0, capacity=0)
push_back 8 floats:
0 (size=1, capacity=1)
0 1.41 (size=2, capacity=2)
0 1.41 2.82 (size=3, capacity=4)
0 1.41 2.82 4.24 (size=4, capacity=4)
0 1.41 2.82 4.24 5.65 (size=5, capacity=8)
0 1.41 2.82 4.24 5.65 7.06 (size=6, capacity=8)
0 1.41 2.82 4.24 5.65 7.06 8.47 (size=7, capacity=8)
0 1.41 2.82 4.24 5.65 7.06 8.47 9.89 (size=8, capacity=8)
insert(3, 99):
0 1.41 2.82 99 4.24 5.65 7.06 8.47 9.89 (size=9,
capacity=16)
insert(0, 98):
98 0 1.41 2.82 99 4.24 5.65 7.06 8.47 9.89
(size=10, capacity=16)
insert(6, 97):
98 0 1.41 2.82 99 4.24 97 5.65 7.06 8.47 9.89
(size=11, capacity=16)
Construct from int array:
6 6 16 14 -6 12 6 10 8 6 6 4 2 0 (size=14, capacity=16)
头文件:
template <typename T>
class vec
{
private:
T* v;
int count; //size
int capacity;
public:
vector(){ //default constructor
capacity=0;
v = nullptr;
count = 0;
}
vector(const vector& b) //copy contructor
{
//..
}
~vector() {//destructor
//..
}
void push_back(const T& t) // contain the capacity
{
if(count+1>capacity)
{
capacity = std::max(2*capacity, 1);
T* newData = new T[capacity];
for(int i=0; i <count; i++)
{
newData[i] = v[i];
}
delete[] v;
v = newData;
}
v[count++] = t;
}
void insert(int index, T number)
{
for (int i = count; i >= index; i--) {
v[i + 1] = v[i];
}
count++;
v[index] = number;
}
template <typename T1>
friend void Print(const vector<T1>& rp); //friend print
friend void Print(const vector<float>& rp);
};
template <typename T1>
void Print (const vector<T1>& rp)
{
for (unsigned i = 0; i < rp.size(); i++)
std::cout << rp[i] << " ";
std::cout << "(size=" << rp.size()<< "," << " " << "capacity="
<< rp.capacity << ")";
std::cout << std::endl;
}
void Print (const vector<float>& rp)
{
for (unsigned i = 0; i < rp.size(); i++)
{
std::cout << std::setw(5) << std::setprecision(3)
<< float (rp[i]) <<" ";
}
std::cout << "(size=" << rp.size()<< "," << " " << "capacity="
<< rp.capacity << ")";
std::cout << std::endl;
}
主文件:
void TestInsert(void)
{
{
cout << "\n********** TestInsert **********\n";
cs150::vector<float> a;
Print(a);
cout << "push_back 8 floats:\n";
for( float i = 0; i < 8; ++i )
{
a.push_back( i * 1.412492f );
Print(a);
}
cout << "insert(3, 99):\n";
a.insert(3, 99);
Print(a);
cout << "insert(0, 98):\n";
a.insert(0, 98);
Print(a);
cout << "insert(6, 97):\n";
a.insert(6, 97);
Print(a);
}
{
int ia[] = {
2, 4, 6, 6, 8, 10, 6, 12, -6, 14, 16, 6, 6
};
std::size_t size = sizeof( ia ) / sizeof( *ia );
std::cout << "Construct from int array:\n";
cs150::vector<int> a;
a.push_back( 0 );
for( std::size_t i = 0; i < size; ++i )
a.insert( 0, ia[i] );
Print(a);
}
}
int main( int argc, char **argv )
{
std::size_t test_num = 0;
if( argc > 1 )
test_num = std::size_t( std::atoi( argv[ 1 ] ) );
typedef void (*Test)( void );
Test Tests[] = {
TestInsert, // 4
};
std::size_t num = sizeof( Tests ) / sizeof( *Tests );
if( test_num == 0 )
{
for( std::size_t i = 0; i < num; ++i )
Tests[ i ]();
}
else if( test_num <= num )
{
Tests[ test_num - 1 ]();
}
return 0;
}