473,721 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector resize(0) vs clear()

Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).

Thanks,
../Chaitanya Atreya

Jul 18 '06 #1
5 32606
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
"atreya" <at****@gmail.c omwrote:
Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).
clear() is potentially more efficient than resize(0), both in terms of
speed and code size.

resize must check whether it is increasing in size or decreasing, and be
prepared to append to the end, or erase from the end (so all of that
code needs to be instantiated).

clear() knows right up front (at vector design time) that it has the
semantics of erase(begin(), end()). Actually calling erase(begin(),
end()) is suboptimal (imho). Indeed if you have a vector<Twhere T has
a trivial destructor (which can be determined at compile time for at
least a subset of types - scalars), then all vector should do is set
size() to 0 - job done. No if statement (which can empty a pipeline),
no looping, just a single assignment statement - maybe two inlined
assembly instructions. -- Implementations may differ.

-Howard
Jul 18 '06 #2

atreya wrote:
Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

Use clear() when you explicitly wish to make it clear you're erasing
all elements of the vector. Use resize() to make it clear you're
changing the size of the vector (either shorter or longer...).
Advantages are clarity, disadvantages are none... Even if the
implementation has a special rule case for an empty vector.

Jon

Jul 18 '06 #3

Jon Clements wrote:
>.Even if the implementation has a special rule case for an empty vector.
I meant to say: if resize(0) checked to see if the resultant size of
the vector should be empty (so it might choose to call clear()). Not if
the vector was already empty.

As Howard has already mentioned, resize needs to be smarter, so if you
want to empty the vector use the method designed for it! Don't worry
about the implementation!

Jon.

Jul 18 '06 #4
Howard Hinnant wrote:
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
"atreya" <at****@gmail.c omwrote:
Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).

clear() is potentially more efficient than resize(0), both in terms of
speed and code size.

resize must check whether it is increasing in size or decreasing, and be
prepared to append to the end, or erase from the end (so all of that
code needs to be instantiated).

clear() knows right up front (at vector design time) that it has the
semantics of erase(begin(), end()). Actually calling erase(begin(),
end()) is suboptimal (imho). Indeed if you have a vector<Twhere T has
a trivial destructor (which can be determined at compile time for at
least a subset of types - scalars), then all vector should do is set
size() to 0 - job done. No if statement (which can empty a pipeline),
no looping, just a single assignment statement - maybe two inlined
assembly instructions. -- Implementations may differ.

-Howard
About resize checking whether it is increasing/decreasing etcetera, and
instantiating code, there is no overhead at all except for a single if
statement as follows:
if (_Newsize < size())
where size() can be inlined (single line).

../Chaitanya Atreya

Jul 18 '06 #5
atreya wrote:
Hi,

I'm trying to figure out if there are any differences between the
implementation of resize(0) and clear() that a programmer using
std::vector should be aware of!

Could there be any advantage/disadvantage of using one over the other?

I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).

Thanks,
./Chaitanya Atreya
I'd go with Jon here: one function, one responsibility. Doing otherwise
is confusing and could lead to surprises. This would be like relying on
the interface to realloc () to allocate, delete and resize memory all in
one function.

Cheers
Jim.
Jul 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7072
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
4
12242
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
7
3713
by: Martin Magnusson | last post by:
I'm having trouble clearing and resizing a static std::vector of std::vectors (segmentation fault). Is it OK to call clear() and resize() on a static attribute? My code is similar to the one posted below. The code below works, though. My original code crashes on the second call to X::f(). My question is: is this code valid, or is it just a coincidence that it doesn't crash?
8
10648
by: Alex Vinokur | last post by:
What is relation between std::vector's reserve() and erase()/clear()? vector<int> v; v.reserve(100); v.resize(100); v.erase(v.end()); How many elements are reserved here: 100 or 99?
1
5483
by: Daniel J Watkins | last post by:
Hi, When I attempt to resize a column in a 2d vector I receive the error listed below. I'm using the libraries supplied with VxWorks. 0xf3fe450 (tExcTask): memPartFree: invalid block 0xf340ef0 in partition 0x311b8c Here is the example code I'm using to produce the error. Any idea's what is wrong with the code?
8
4908
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike std::vector::erase, it calls T::operator= only three times no matter
9
5119
by: Gernot Frisch | last post by:
Hi, I want to be able to write: class foo { std::vector<intm_i (64); }
4
2317
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the elements of the vector. Thank you ! Mathieu Code:
23
3473
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12) The program below fails, but if the reserve(en)
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.