简体   繁体   English

你能截断 std::vector<int> 在恒定的时间?</int>

[英]Can you truncate std::vector<int> in constant time?

There are several ways to make a vector empty, including有几种方法可以使向量为空,包括

std::vector<int> example = makeMyReallyBigVector();
example.clear();
example.resize(0);
example.erase(example::being(), example::end());
// any other method which would be relevant

Are there any guarantees in the C++ standard about which is most efficient, time-wise? C++ 标准中是否有关于哪个最有效、时间最明智的保证? The contents are a primitive data type without destructor.内容是没有析构函数的原始数据类型。 What I am especially concerned about is, I don't want the vector capacity to change, I just want it's internal "used size" set to 0 without touching the now-erased contents.我特别关心的是,我不希望向量容量发生变化,我只希望它的内部“已用大小”设置为 0,而不触及现在已删除的内容。

What I want is to set the int vector size to 0 in constant time, without freeing any memory. Is this possible with C++ standard?我想要的是在恒定时间内将int向量大小设置为 0,而不释放任何 memory。C++ 标准是否可行?

If the C++ standard gives no guarantees, I'm using GNU C++ standard library, so if standard doesn't, does that give any guarantees?如果 C++ 标准不提供任何保证,我正在使用 GNU C++ 标准库,那么如果标准不提供任何保证? For sake of portability, in this case also information about the Clang and Microsoft standard libraries would of interest, of course.为了便于携带,在这种情况下,当然也会对有关 Clang 和 Microsoft 标准库的信息感兴趣。

example.clear();

clear is: clear的是:

Linear in the size of the container, ie, the number of elements.与容器的大小成线性关系,即元素的数量。

example.resize(0);

resize is resize

Linear in the difference between the current size and count.当前大小和计数之间的差异呈线性。 Additional complexity possible due to reallocation if capacity is less than count如果容量小于计数,则重新分配可能会导致额外的复杂性

example.erase(example::begin(), example::end());

erase is erase

Linear: the number of calls to the destructor of T is the same as the number of elements erased, the assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements线性:调用T的析构函数的次数与被擦除的元素个数相同,T的赋值运算符被调用的次数等于vector中被擦除元素之后的元素个数

I am not sure why neither clear nor resize mention the dependence on the destructor.我不确定为什么clearresize都没有提到对析构函数的依赖。 int has only a pseudo-destructor (something that makes calling the destructor on an int valid, but it doesn't do anything). int只有一个伪析构函数(使对int有效调用析构函数的东西,但它什么都不做)。

If the elements do have a destructor the complexitiy is linear.如果元素确实具有析构函数,则复杂性是线性的。 For int the compiler may be clever enough to do it in constant time.对于int ,编译器可能足够聪明,可以在恒定时间内完成。 When you are not sure about runtime you can profile the run, or directly read the disassembly code.当您不确定运行时间时,您可以分析运行情况,或直接阅读反汇编代码。

example.clear(); may be faster.可能会更快。 All of the above examples take arguments, which means that additional code will be generated to pass them to the function, which will need to be executed.以上所有示例都采用 arguments,这意味着将生成额外的代码将它们传递给需要执行的 function。

However, it's important to remember that the C++ compiler is pretty smart and might be able to turn example.resize(0);但是,重要的是要记住 C++ 编译器非常聪明,可能能够将example.resize(0); into example.clear();进入example.clear(); . . But it is not exactly.但事实并非如此。 So it's better to help the compiler, if not difficult.因此,如果不困难,最好帮助编译器。

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

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