简体   繁体   English

减少数组的大小

[英]Reduce an array in size

I had an array with a lot of numbers and I had to "remove" some of them so I decided to put zeros instead which I could just ignore latter on and so I got this: 我有一个包含很多数字的数组,我不得不“删除”其中的一些数字,因此我决定放零,而我可以忽略后者,因此得到了这一点:

0033033055000022220440044000 0033033055000022220440044000

But now I realize I need to add the n-1 to the original size, for example I have 5 so I need to add the number before it which is 3 , the problem now is I don't need the zeros. 但是现在我意识到我需要将n-1加到原始大小,例如我有5所以我需要在它之前加上数字3 ,现在的问题是我不需要零。

Therefore, is there a way to reduce an array in size/remove unneeded indexes or would it just simpler to write a function which would loop until it found an integer? 因此,有没有办法减小数组的大小/删除不需要的索引,还是编写一个循环直到找到整数的函数更简单?

std::vector<int> values;
values.erase(std::remove(values.begin(), values.end(), 0));
values.shrink_to_fit();

std::remove puts all elements which match the given value (0 in this case) in the end of the vector, and returns an iterator to the start of the removed elements. std::remove将与给定值(在这种情况下为0)匹配的所有元素放在向量的末尾,并将迭代器返回到被删除元素的开头。 Erase then takes this iterator and removes all subsequent values from the vector. 然后,擦除将使用此迭代器,并从向量中删除所有后续值。 shrink_to_fit then attempts to reallocate the vector with a smaller amount of memory, taking into account the reduced number of elements. 然后,shrink_to_fit考虑到元素数量的减少,尝试使用较少的内存重新分配向量。 Unless you are removing thousands of elements, it's not really worth shrinking the vector. 除非要删除成千上万个元素,否则缩小向量的确不值得。

What you are trying to accomplish is possible. 您试图实现的目标是可能的。 By using malloc() and realloc() you can dynamically set the size of the array you're using, thus allowing you to grow/shrink arrays as you need to. 通过使用malloc()和realloc(),您可以动态设置正在使用的数组的大小,从而允许您根据需要增大/缩小数组。

It's complicated, and requires careful pointer management. 它很复杂,需要仔细的指针管理。 If that is the path you'd like to go down, then there are several tutorials online for things like that. 如果这是您想走的路,那么在线上有几本针对此类事情的教程。 This was the first thing I found with a google search, and seems to give the basic idea behind how to do what you want to do. 是我在Google搜索中发现的第一件事,似乎为如何做自己想做的事情提供了基本思路。

That being said, Don't do that. 话虽这么说,不要那样做。 the standard library (STD) has several different tools to store data that would be better suited to the task. 标准库(STD)具有几种不同的工具来存储更适合任务的数据。 Vectors are a classic choice and work almost exactly like arrays, but they are generally poor when you are removing items from the middle of them. 向量是一种经典的选择,几乎可以像数组一样工作,但是当您从它们中间删除项目时,它们通常较差。 Lists are REALLY good at deleting items in the middle of them, but traversing them can be a pain, as you have to start at the beginning and work your way through (you can't access things in the middle willy nilly at will.) Choose whichever of these suit your needs better, and use those. 列表真的很擅长删除中间的项目,但是遍历它们可能会很痛苦,因为您必须从头开始并逐步解决(您不能随意访问中间的内容)。从您的需求中选择更适合您的需求,然后使用它们。 It will simplify your life. 它将简化您的生活。

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

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