简体   繁体   English

c ++向量不像数组

[英]c++ vectors not like arrays

I've been getting my head round c++ for a few months, and have been directed by google to stack overflow most of the time for c++ queries. 我已经开始使用c ++几个月了,并且已经被谷歌指导到堆栈溢出大部分时间用于c ++查询。 I noted frequently exhortations of the type "why don't you use a vector", and was inspired to do just that. 我经常注意到“你为什么不使用矢量”这一类型的劝诫,并且受到了启发而做到了这一点。

So, primarily to get the minor benefits of automatic memory deallocation, and being able to write typed comparison functions for sorting. 因此,主要是为了获得自动内存释放的微小优势,并能够编写用于排序的类型化比较函数。 I switched an array of pointers to objects to being a vector. 我将一个指向对象的数组切换为一个向量。 Now I thought (incorrectly it seems) that vectors could be used more or less like arrays, and hence I initialized thusly: 现在我想(似乎不正确)矢量可以或多或少地像数组一样使用,因此我初步化了:

cluster  clusters[LOTS];
vector<cluster *> pclust;
pclust.reserve(numClust);
for (int i=0; i<numClust; ++i)
    pclust[i] = clusters + i;

No complaints from the compiler. 没有来自编译器的投诉。 Then some time later I need to sort the vector on some attribute of the cluster object. 然后一段时间后我需要在集群对象的某个属性上对向量进行排序。 So: 所以:

std::sort(pclust.begin(), pclust.end(), ClusterCompareNumSegs);

Again no problems compiling. 再次编译没有问题。 Except the vector doesn't get sorted. 除了矢量没有得到排序。 It turns out that vector.size() is zero, and of course my initialization should have been 事实证明,vector.size()为零,当然我的初始化应该是

pclust.push_back(clusters + i);

Now that's easy to fix, but I am confused, because the initial incorrect assignment was working. 现在这很容易解决,但我很困惑,因为最初的错误分配工作正常。 I successfully iterated through the vector - using the array syntax, like so: 我成功迭代了向量 - 使用数组语法,如下所示:

for (clustind=0; clustind < numClust; ++clustind) {<br>
    cluster *cl = pclust[clustind];
    ...happily access *cl...

And it all worked fine. 这一切都很好。 So I'm just wondering what's going on. 所以我只是想知道发生了什么。 Presumably in my initial assignments, I was trying to access elements not yet in the vector (I was trying to put them in), and the vector was throwing exceptions that I was ignoring. 据推测,在我最初的任务中,我试图访问尚未在向量中的元素(我试图将它们放入),并且向量抛出了我忽略的异常。 But nonetheless, when referencing the locations, the pointers were there. 但是,在引用位置时,指针就在那里。 Can anyone provide enlightenment? 任何人都可以提供启发吗?

vector::reserve doesn't change the size of your vector, it still contains only the 0 elements it was created with. vector::reserve不会改变矢量的大小,它仍然只包含它创建的0元素。 What it does is make sure that the vector can potentially hold numClust without having to reallocate. 它的作用是确保向量可以保留numClust而不必重新分配。 See here . 看到这里

What you want is to either declare the vector to have that size 你想要的是声明矢量具有那个大小

vector<cluster *> pclust(numClust);

or to resize the vector 或者调整矢量大小

pclust.resize(numClust);

std::vector::reserve requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements . std::vector::reserve 请求向量容器的元素的已分配存储空间的容量至少足以容纳n个元素 It doesn't resize the vector, that's what std::vector::resize does. 它没有调整向量的大小,这就是std::vector::resize作用。

Replace pclust.reserve(numClust); 替换pclust.reserve(numClust); with pclust.resize(numClust); pclust.resize(numClust); .

Alternatively you could remove pclust.reserve(numClust); 或者你可以删除pclust.reserve(numClust); call and change construction of this vector to: vector<cluster *> pclust(numClust); 调用并将此向量的构造更改为: vector<cluster *> pclust(numClust); which yields same result. 产生相同的结果。

I also suggest you to have a look at this question: std::vector reserve() and push_back() is faster than resize() and array index, why? 我还建议你看看这个问题: std :: vector reserve()和push_back()比resize()和数组索引更快,为什么? :) :)

operator[] used with vector returns reference to element on index-position. 与vector一起使用的operator []返回对index-position元素的引用。 BUT, you haven't initialized vector with any values, so it was empty. 但是,您没有使用任何值初始化向量,因此它是空的。

Altough you did a pclust.reserve(numClust) , but it only tells that size of the vector will change soon and it allocates storage space without changing vector's size yet. 你做了一个pclust.reserve(numClust) ,但它只是告诉我们,矢量的大小很快就会改变,并且它会在不改变矢量大小的情况下分配存储空间。

cluster  clusters[LOTS];
vector<cluster *> pclust(numClust);
for (int i = 0; i < numClust; ++i)
  pclust[i] = clusters + i;

But this mean you are still using an array to store clusters. 但这意味着您仍在使用数组来存储集群。 Can't you make clusters a vector ? 你不能把clusters变成矢量吗?

vector<cluster> clusters(LOTS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM