473,791 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list<T> size

Does a list<Tkeep track of its size or is the size re-calculated every
time you call list<T>.size()?
Oct 9 '07 #1
7 1948
barcaroller wrote:
Does a list<Tkeep track of its size or is the size re-calculated every
time you call list<T>.size()?
The standard allows for either method. As a consequence, you should call
list<>::size() only when you have to. In particular, prefer

list<>::empty()

to

list<>::size() == 0

Best

Kai-Uwe Bux
Oct 10 '07 #2
barcaroller wrote:
Does a list<Tkeep track of its size or is the size re-calculated every
time you call list<T>.size()?
IIRC std::list::size () is *not* guaranteed to be constant-time, and in
most implementations it's indeed linear-time.

I think it has something to do with splice(), but I don't remember the
details.
Oct 10 '07 #3
On 2007-10-10 02:28, Juha Nieminen wrote:
barcaroller wrote:
>Does a list<Tkeep track of its size or is the size re-calculated every
time you call list<T>.size()?

IIRC std::list::size () is *not* guaranteed to be constant-time, and in
most implementations it's indeed linear-time.

I think it has something to do with splice(), but I don't remember the
details.
I think splice must run in O(n), where n is the number of spliced
elements, and that does not allow for recalculating the size when splicing.

--
Erik Wikström
Oct 10 '07 #4
Erik Wikström wrote:
I think splice must run in O(n), where n is the number of spliced
elements, and that does not allow for recalculating the size when splicing.
Don't you mean O(1)?
Oct 10 '07 #5
Hi

Juha Nieminen wrote:
Erik Wikström wrote:
>I think splice must run in O(n), where n is the number of spliced
elements, and that does not allow for recalculating the size when
splicing.

Don't you mean O(1)?
....where 1 is the number of spliced elements.

SCNR :-)

(It should be O(1) of course)
Markus
Oct 10 '07 #6
On 2007-10-10 15:36, Juha Nieminen wrote:
Erik Wikström wrote:
>I think splice must run in O(n), where n is the number of spliced
elements, and that does not allow for recalculating the size when splicing.

Don't you mean O(1)?
I was thinking of the version taking a position iterator, a list and two
iterators into this list. It is linear if the provided list is not
*this. Problem is I can no longer remember the reason why this prevents
size() from being O(1).

--
Erik Wikström
Oct 10 '07 #7
On 2007-10-10 05:08:10 -1000, Erik Wikström <Er***********@ telia.comsaid:
On 2007-10-10 15:36, Juha Nieminen wrote:
>Erik Wikström wrote:
>>I think splice must run in O(n), where n is the number of spliced
elements, and that does not allow for recalculating the size when splicing.

Don't you mean O(1)?

I was thinking of the version taking a position iterator, a list and two
iterators into this list. It is linear if the provided list is not
*this.
It's linear if the provided list has a different allocator from *this.
If the allocators are the same, splice does just what it's name
suggests: it munges a few pointers to snip the specified sublist out of
its current container and insert it directly into the target.
Problem is I can no longer remember the reason why this prevents
size() from being O(1).
The problem is that if splice only needs to munge the end pointers
(i.e. the allocators are the same) it doesn't know how many elements
were inserted.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 10 '07 #8

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

Similar topics

14
5635
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
4
53016
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
5
1922
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count; pageNumber = 1; }
0
1753
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57556
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
35
5895
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
9
6186
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50 or so or it can contain as much 10000000 bytes. In reality, I do not know the maximum number of bytes. In my function, I am going to read() the byte stream using a buffer. Now, is it better to read it into a buffer and dump the buffer into a...
0
9669
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
9515
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
10207
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...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
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...
0
9029
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.