3

尝试删除指针向量时遇到问题:

std::vector<float*> *v;

v = new std::vector<float*>;
v->assign(2, new float[2]);

for (int i = 0; i < 2; ++i)
{
    delete[] v->at(i);
}
delete v;

我正在从整个向量中删除每个元素,但我仍然得到一个断言。你能告诉我我做错了什么吗?

先感谢您。

4

3 回答 3

5
于 2013-09-20T09:49:55.020 回答
1

THe v->assign(2, new float[2]) does the same as :

float *f = new float[2];
for(int i = 0; i < 2; i++)
   v->push_back(f);

of course, that's MOST likely not what you want - you probably want:

for(int i = 0; i < 2; i++)
{
   float *f = new float[2];
   v->push_back(f);
}

And using new on vector is just plain wrong - just use a plain vector. Inside it, put a vector<float> - or, if you just want two elements every time, use something like:

struct f2 { float a; float b; };
vector<struct f2> v;
于 2013-09-20T09:57:10.733 回答
1

In general, having raw owning pointers is not a good idea (unless in special cases, like when you are defining some custom high-performance highly-specialized data structure).

In your code - unless you are in special cases - there should be no explicit calls to new and delete, in modern C++11/14.

Your code sample style seems more like Java and other garbage-collection-based reference-semantics-based languages style. Instead, C++ tends to prefer value semantics (e.g. prefer: MyClass x; to MyClass * px = new MyClass();, and if you really need some owning pointer, use smart pointers like std::shared_ptr or std::unique_ptr):

// Your original code:
//
//   std::vector<float*> *v;
//   v = new std::vector<float*>;
//
// Not good, since:
// 
// 1. You have a std::vector of owning pointers
//    (std::vector<float *>)
//
// 2. You have a raw owning pointer for the containing std::vector itself
//    (v = new std::vector<....>)
//

A more modern and correct way of writing your code can be using a vector of vectors (instead of a vector of raw owning pointers float*):

//
// Vector of vectors (i.e. 2D matrix), allocated on the stack
// Note: no raw owning pointers here.
//
vector<vector<float>> v;

Then you can use use std::vector::push_back() or some other std::vector methods to populate the vector.


As a more high-performance and less-overhead alternative for a 2D matrix, you could use a single 1D std::vector, and linearize the content of the 2D matrix in a single 1D contiguous vector, of size Rows * Columns, e.g.:

vector<float> matrix;
matrix.resize( Rows * Columns );

And to access element at position (rowIndex, columnIndex), you can use a formula like this (if you store matrix elements row-wise, i.e. row#1, row#2, ..., row#N):

indexInVector = columnIndex + rowIndex * Columns;

All this can be nicely wrapped in a class template template <typename T> class Matrix {...};, with proper methods to read and write matrix elements, and the containing std::vector<T> as data member.

于 2013-09-20T10:17:44.183 回答