473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

order of STL functions

Hi,

Is there a place on the web that specifies the guaranteed order of STL
functions? In particular, I want to know if std::vector::resize(x) is
guaranteed to be constant running time. Obviously std::vector::resize(x, y)
is O(n).

Oct 16 '05 #1
9 1347
In the worst case scenario, resize may have to copy all of the elements
of a vector due to a vector's guarentee that all of it's elements are
contiguous.

I don't see how any such guarentee can be given.

Oct 16 '05 #2

An**********@gmail.com wrote:
In the worst case scenario, resize may have to copy all of the elements
of a vector due to a vector's guarentee that all of it's elements are
contiguous.

I don't see how any such guarentee can be given.


Any such running time order guarentee that is.

Oct 16 '05 #3
> In the worst case scenario, resize may have to copy all of the elements
of a vector due to a vector's guarentee that all of it's elements are
contiguous.

I don't see how any such guarentee can be given.


Ah I forgot to mention a detail: in the beginning, I resize the vector to
the maximum elements that will ever be allocated. But I may resize down to
a smaller size at a later time, and then back up--but never past the
originally specified max size.
Oct 16 '05 #4

"vsgdp" <sp**@void.com> wrote in message
news:f%A4f.2705$i%.2491@fed1read07...
In the worst case scenario, resize may have to copy all of the elements
of a vector due to a vector's guarentee that all of it's elements are
contiguous.

I don't see how any such guarentee can be given.


Ah I forgot to mention a detail: in the beginning, I resize the vector to
the maximum elements that will ever be allocated. But I may resize down
to a smaller size at a later time, and then back up--but never past the
originally specified max size.


So you know the largest possible size. If you want
best performance, create the vector with that size
from the start, and leave it that size.

std::vector<int>(max_size);

/* do your stuff */

-Mike
Oct 17 '05 #5
So you know the largest possible size. If you want
best performance, create the vector with that size
from the start, and leave it that size.

std::vector<int>(max_size);

/* do your stuff */
Yes, but I would like the "size" to change but not the capacity. I still
want to use push_back and pop_back to increment/decrement the size. I just
don't want any memory allocations to occur other than the first one.

Does anyone know if there are rules defined for when the capacity can
change? Obviously it must change if you need _more_ memory, but would
std::vector actally delete an array and allocate a smaller sized array if
the size was small relative to the capacity?
-Mike

Oct 17 '05 #6
vsgdp wrote:
So you know the largest possible size. If you want
best performance, create the vector with that size
from the start, and leave it that size.

std::vector<int>(max_size);

/* do your stuff */


Yes, but I would like the "size" to change but not the capacity. I still
want to use push_back and pop_back to increment/decrement the size. I
just don't want any memory allocations to occur other than the first one.

Does anyone know if there are rules defined for when the capacity can
change? Obviously it must change if you need _more_ memory, but would
std::vector actally delete an array and allocate a smaller sized array if
the size was small relative to the capacity?


The effect of resize() is defined by the standard to be the same as:

if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size())
erase(begin()+sz, end());
else
;

You are in the case of sz < size(). The complexity of erase is linear in the
length of the range that is erased. The way I understand the standard, the
complexity of resize() is therefore bounded by the amount of elements you
erase. (You cannot do better since you are calling the destructors for
these elements.)

Circumstantial evidence that nothing unexpected will happen is the
following: reallocation should not be triggered by erase() since it is not
allowed to invalidate iterators before the first erased elements.
Best

Kai-Uwe Bux
Oct 17 '05 #7
vsgdp wrote:
So you know the largest possible size. If you want
best performance, create the vector with that size
from the start, and leave it that size.

std::vector<int>(max_size);

/* do your stuff */

Yes, but I would like the "size" to change but not the capacity. I still
want to use push_back and pop_back to increment/decrement the size. I just
don't want any memory allocations to occur other than the first one.


Look at std::vector::reserve.

e.g.

// calculate maximum size
std::vector<int>::size_t max_size = calculate_max();
std::vector<int> myvec;
myvec.reserve(max_size);

After this, myvec.size() == 0, but it has capacity of max_size.

Oct 17 '05 #8
vsgdp <sp**@void.com> wrote:
Hi,

Is there a place on the web that specifies the guaranteed order of STL
functions? In particular, I want to know if std::vector::resize(x) is
guaranteed to be constant running time. Obviously std::vector::resize(x, y)
is O(n).


In section 17.1.2 of TC++PL:SE, there is this table (see the book for
explanations:

Standard Container Operations
+----------------+-----------+------------+------------+--------------+-----------+
| | [] | List | Front | Back (Stack) | Iterators |
| | | Operations | Operations | Operations | |
+----------------+-----------+------------+------------+--------------+-----------+
| vector | const | O(n)+ | | const+ | Ran |
| list | | const | const | const | Bi |
| deque | const | O(n) | const | const | Ran |
+----------------+-----------+------------+------------+--------------+-----------+
| stack | | | | const | |
| queue | | | const | const | |
| priority_queue | | | O(log(n)) | O(log(n)) | |
+----------------+-----------+------------+------------+--------------+-----------+
| map | O(log(n)) | O(log(n))+ | | | Bi |
| multipmap | | O(log(n))+ | | | Bi |
| set | | O(log(n))+ | | | Bi |
| multiset | | O(log(n))+ | | | Bi |
+----------------+-----------+------------+------------+--------------+-----------+
| string | const | O(n)+ | O(n)+ | const+ | Ran |
| array | const | | | | Ran |
| valarray | const | | | | Ran |
| bitset | const | | | | |
+----------------+-----------+------------+------------+--------------+-----------+

--
Marcus Kwok
Oct 17 '05 #9
vsgdp <sp**@void.com> wrote:
Hi,

Is there a place on the web that specifies the guaranteed order of STL
functions? In particular, I want to know if std::vector::resize(x) is
guaranteed to be constant running time. Obviously std::vector::resize(x, y)
is O(n).


In section 17.1.2 of TC++PL:SE, there is this table (see the book for
explanations):

Standard Container Operations
+----------------+-----------+------------+------------+--------------+-----------+
| | [] | List | Front | Back (Stack) | Iterators |
| | | Operations | Operations | Operations | |
+----------------+-----------+------------+------------+--------------+-----------+
| vector | const | O(n)+ | | const+ | Ran |
| list | | const | const | const | Bi |
| deque | const | O(n) | const | const | Ran |
+----------------+-----------+------------+------------+--------------+-----------+
| stack | | | | const | |
| queue | | | const | const | |
| priority_queue | | | O(log(n)) | O(log(n)) | |
+----------------+-----------+------------+------------+--------------+-----------+
| map | O(log(n)) | O(log(n))+ | | | Bi |
| multipmap | | O(log(n))+ | | | Bi |
| set | | O(log(n))+ | | | Bi |
| multiset | | O(log(n))+ | | | Bi |
+----------------+-----------+------------+------------+--------------+-----------+
| string | const | O(n)+ | O(n)+ | const+ | Ran |
| array | const | | | | Ran |
| valarray | const | | | | Ran |
| bitset | const | | | | |
+----------------+-----------+------------+------------+--------------+-----------+

--
Marcus Kwok
Oct 17 '05 #10

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

Similar topics

11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
14
by: Joerg Schuster | last post by:
Hello, according to http://mail.python.org/pipermail/tutor/2001-July/007246.html the order of function definitions does matter in python. Does anyone know a trick to avoid this? Is there a...
6
by: Ian Sparks | last post by:
I have a python file with a number of functions named with the form doX so : doTask1 doThing doOther The order these are executed in is important and I want them to be executed top-down. They...
12
by: Matias Silva | last post by:
I have this list that i'm ordering by units_unit_id and is there way I can get a natural order so that D4 and D5 comes before D12 Thanks, Matt +---------------+ | units_unit_id |...
2
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
25
by: tienlx | last post by:
Hi all, i'm sorry about my poor English ( it isn't my native language). I confused the order which the function in C be evaluted in for() function ? Ex: i have 3 functions : a() b() and c() and...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
13
by: Thomas Mlynarczyk | last post by:
Hi, I have this code: class Test { public $status = 'dead'; function __construct() { $this->status = 'alive'; } function __destruct() { echo '<br>__destruct()'; } }
7
by: Ravi | last post by:
Suppose we are given a statement: f1(23,1) * f2(3) + f(4) can you please tell what is the order of evaluation of these functions?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.