I am trying to understand under what circumstances destructors get called
with std::vector. I have an application in which I will put real objects,
not just pointers, in the vector.
1. The standard says that empty() has constant complexity. If it actually
called the destructor for each object, it seems to me it would have
linear complexity. Does empty() call the destructor for each object in the
container? If yes, why is it described as having constant commplexity?
If no, that seems like a problem too.
2. The standard describes clear by saying that it behaves like
erase(begin, end). That seems as if it would erase the first element, copying
all succedding elements over it, and so on, which is obviously very
inefficient. Is it safe to assume something more reasonable is happening
under the covers?
3. Does erase call the destructor for the elements erased?
More properly, I guess I should ask if the elements freed at the end after
being moved over erased items are cleared. I'd probably only be erasing
the whole vector or its last element.
As I'm writing this I realize I may have a basic confusion, and that
constructors and destructors are only called at the beginning and end
of the vector's life (and when its capacity expands). The rest of the
time elements are either in use or not, with the behavior of the assignment
operator being key. Is that what's really going on?
I'm interested both in what can and can't be inferred from the standard, and
in what current compiler practice on different platforms is--in other words,
what's safe to assume in portable code.
Jul 22 '05
12 1820
> > But since you brought up the topic I think that the standard requires size() to be O(1) and allows splice on different lists to be O(n).
In a note (bottom of 23.1 Table 65) it says size() "should have constant complexity", it requires that splice has complexity "Contant Time" 23.2.2.4/6
So I was wrong splice() is required to be O(1), I was under the impression that this was upto the implementor, but this is the first time I've checked the standard.
Rob. -- http://www.victim-prime.dsl.pipex.com/
23.2.2.4/14 is the exception that allows linear time for splice in certain
circumstances.
john
John Harrison wrote in
news:c0******** *****@ID-196037.news.uni-berlin.de: > > But since you brought up the topic I think that the standard > requires size() to be O(1) and allows splice on different lists to > be O(n). > In a note (bottom of 23.1 Table 65) it says size() "should have constant complexity", it requires that splice has complexity "Contant Time" 23.2.2.4/6
So I was wrong splice() is required to be O(1), I was under the impression that this was upto the implementor, but this is the first time I've checked the standard.
23.2.2.4/14 is the exception that allows linear time for splice in certain circumstances.
Thanks, I should have read further ;)
Rob.
-- http://www.victim-prime.dsl.pipex.com/
Thanks to everyone for their replies.
On Thu, 12 Feb 2004 04:55:14 +0000, Victor Bazarov wrote: "Ross Boylan" <Ro********@sta nfordalumni.org > wrote... I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector.
1. The standard says that empty() has constant complexity. If it actually called the destructor for each object, it seems to me it would have linear complexity. Does empty() call the destructor for each object in the container? If yes, why is it described as having constant commplexity? If no, that seems like a problem too.
'empty' is not the same as 'clear'. Do not confuse the two. For C++ library containers, 'empty' is an adjective, not a verb.
Oops. I didn't read closely enough, and am used to smalltalk where it
would be isEmpty.
.... 3. Does erase call the destructor for the elements erased?
Yes.
More properly, I guess I should ask if the elements freed at the end after being moved over erased items are cleared.
I am not sure I completely understand the sentence, but elements can be freed even in the middle of erasing, during the move. std::vector is a tricky container, it has to reallocate and move things all the time, so the old elements will get destroyed right after new copies of them are made.
Minor explanation: Suppose you have a 5 element vector and erase the
3rd element. My first wording implied the destructor would be called
on the 3rd element; my refinement acknowledged that 3rd and 4th get
assigned to while the 5th is the one that would be destroyed.
..... Why not trace all the constructor and destructor calls? It's fun and it's educational. It's educational fun.
But it won't tell me what's in the standard or what the range of
actual implementations are. I'm interested both in what can and can't be inferred from the standard, and in what current compiler practice on different platforms is--in other words, what's safe to assume in portable code.
Nothing is safe to assume except what's said in the Standard.
In my experience, not even that, since there are many non-conforming
implementations .
I missed the section 23.2.4.3 about destructors being called that Ivan
so helpfully cited. Unfortunately, that is in a discussion of
complexity; read narrowly, it is thus no guarantee of sematics. I
wish there were an explicit statement about the semantics of erase and
similar operations that shrink the collection size calling
destructors. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rajesh Garg |
last post by:
Can we have private constructors and destructors? IF yes what is the
use of such constructors or destructors.....in the sense where can
these be implemented in a system.................
I have an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...
Regards
RVG
rajeshgarg@opussoft.com
|
by: matthurne |
last post by:
I am working through exercise 8-2 in Accelerated C++...I am
implementing the function equal(b, e, b2) where b is an iterator for
the first element in a container, e is an iterator pointing to one
past the last element in that same container, and b2 is an iterator
for the first element in the second container. Here's what I have:
template <class T>
bool equal(T begin, T end, T begin2) {
while (begin != end) {
if (*begin != *begin2) {
|
by: graftonfot |
last post by:
Hi there,
I've programmed in C++ for many years, but I've been away from it for a
while.
I have some JDK 1.4 code that I need to re-implement in C++. I am
wondering, are there any commonly-used libraries for replacing the Java
SDK types (particularly all the containers) that people use these days?
I know about the STL, but was wondering if there was anything more
popular/current.
|
by: Steven T. Hatton |
last post by:
Now that I have a better grasp of the scope and capabilities of the C++
Standard Library, I understand that products such as Qt actually provide
much of the same functionality through their own libraries. I'm not sure
if that's a good thing or not. AFAIK, most of Qt is compatable with the
Standard Library. That is, QLT can interoperate with STL, and you can
convert back and forth between std::string and Qt::QString, etc.
Are there any...
|
by: Tony Johansson |
last post by:
Hello!
Is it possible to mix C++ code for example DLL developed with Visual Studio
6.0 with C++ code developed with Windows Forms Application(.NET)?
Is it possible to mix C++ code for example DLL developed with Visual Studio
6.0 with C# code developed with Windows Forms Application(.NET)?
Have an application developed with Windows Forms Application(.NET) any
advantages compared to an
| |
by: Shark |
last post by:
I read that in the C++ standard library destructors are not supposed to
throw exceptions. Is this true of only the library, or is it true of
C++ classes in general?
|
by: alternativa |
last post by:
Hello,
I have a few questions concerning classes.
1)
Why some people use default constructos, i.e constructors with no
parameters? To me it doesn't make any sense, is there something I
should know? For example, I'd declare a class in a following way:
class Sample {
int number;
string title;
|
by: Digital Puer |
last post by:
I'm not the world's greatest C++ programmer, so I had a hard
time with these. Some help would be appreciated.
1. Comment on the declaration of function Bar() below:
class Foo
{
static int Bar(int i) const;
}
(I said that since Bar is a static method, it can only access
|
by: AzizMandar |
last post by:
C++ Event Coding Questions
I have done some simple programs in C++ and read a lot of good C++
books (Including The C++ Programing Language, and C++ Primer) I am
trying to understand and implement an Event based program and Message
system. I have a very basic event engine that I'm feeling works a bit
backwards. I'm looking for documents, source code, and books that may
help me better understand how to implement this type of code. I am...
|
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...
|
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...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |