472,983 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

vector iterators ...


typedef std::vector < std::complex < double > > complex_vec_type;

// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc[ 0 ], &cc[ 12000 ] );
}

versus

// option 2
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( cc.begin(), cc.begin()+12000 );
}

Does reallocation occur during construction of dd for either option?

Now if I interpret the standard, more specifically the statement ..

The constructor template <class InputIterator> vector(InputIterator
first, InputIterator last) makes only N calls to the copy construc-
tor of T (where N is the distance between first and last) and no
reallocations if iterators first and last are of forward, bidirec-
tional, or random access categories. It does at most 2N calls to
the copy constructor of T and logN reallocations if they are just
input iterators, since it is impossible to determine the distance
between first and last and then do copying.
brigns to mind the question of what are vector iterators? If memory
serves, they meet requirements of output / input / forward and
bidirectional iterators. With regards to option 2, I'm assuming no
reallocation occurs since iterators first and last are 'forward' (
correct ? ) iterators.

Things get muddled though with the statement " does at most 2N calls
to
the copy constructor of T and logN reallocations if they are just
input iterators"
Now vector iterators meet the category of input iterators hence, what
is 'cc.begin()'? If cc.begin is an input interator then reallocations
does occur during the construction of dd. I'm a little confused.

Jun 3 '06 #1
2 2286
ma740988 wrote:
typedef std::vector < std::complex < double > > complex_vec_type;

// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc[ 0 ], &cc[ 12000 ] );
}

versus

// option 2
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( cc.begin(), cc.begin()+12000 );
}

Does reallocation occur during construction of dd for either option?
No.
Now if I interpret the standard, more specifically the statement ..

> The constructor template <class InputIterator> vector(InputIterator
first, InputIterator last) makes only N calls to the copy construc-
tor of T (where N is the distance between first and last) and no
reallocations if iterators first and last are of forward, bidirec-
tional, or random access categories. It does at most 2N calls to
the copy constructor of T and logN reallocations if they are just
input iterators, since it is impossible to determine the distance
between first and last and then do copying.

brigns to mind the question of what are vector iterators? If memory
serves, they meet requirements of output / input / forward and
bidirectional iterators. With regards to option 2, I'm assuming no
reallocation occurs since iterators first and last are 'forward' (
correct ? ) iterators.
They are random access iterators.
Things get muddled though with the statement " does at most 2N calls
to
the copy constructor of T and logN reallocations if they are just
input iterators"
Now vector iterators meet the category of input iterators hence, what
is 'cc.begin()'? If cc.begin is an input interator then reallocations
does occur during the construction of dd. I'm a little confused.


Random access iterators certainly meet the requirements for input
iterators but they most definitely are not "just input iterators". So
this paragraph does not apply.

Jun 3 '06 #2
On Sat, 03 Jun 2006 08:51:23 -0700, ma740988 wrote:
typedef std::vector < std::complex < double > > complex_vec_type;

// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc[ 0 ], &cc[ 12000 ] );
}

versus

// option 2
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( cc.begin(), cc.begin()+12000 );
}

Does reallocation occur during construction of dd for either option?
No. You get one allocation of sufficient size at the beginning, since the
compiler can know how many elements there will be.

Now if I interpret the standard, more specifically the statement ..

> The constructor template <class InputIterator> vector(InputIterator
first, InputIterator last) makes only N calls to the copy construc- tor
of T (where N is the distance between first and last) and no
reallocations if iterators first and last are of forward, bidirec-
tional, or random access categories. It does at most 2N calls to the
copy constructor of T and logN reallocations if they are just input
iterators, since it is impossible to determine the distance between
first and last and then do copying.

brigns to mind the question of what are vector iterators? If memory
serves, they meet requirements of output / input / forward and
bidirectional iterators. With regards to option 2, I'm assuming no
They're also RandomAccess iterators too.
reallocation occurs since iterators first and last are 'forward' ( correct
? ) iterators.
RandomAccess.

Things get muddled though with the statement " does at most 2N calls to
the copy constructor of T and logN reallocations if they are just input
iterators"
Now vector iterators meet the category of input iterators hence, what is
'cc.begin()'? If cc.begin is an input interator then reallocations does
occur during the construction of dd. I'm a little confused.


Note that it said *just* input iterators. Since vector iterators are
"more" than just input iterators (RandomAccess, to be specific), the
compiler knows how many elements that there will be, and will size the new
vector appropriately.

If we're dealing with only an input iterator (such as istream_iterator),
the compiler cannot determine how many elements there will be before
starting to copy. With forward iterators, the compiler can safely advance
through the range to count how many elements there will be, then start
again at the first iterator. With input iterators, the compiler cannot
"rewind" the range to get back to the beginning, so it must insert the
elements into the new vector as it is going through the range the first
time around.
Jun 3 '06 #3

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

Similar topics

11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
13
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
9
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
2
by: ernesto | last post by:
Hi: I want to create my own vector class; I want to provide methods like: class Vector { public: void add(const Object* aVal); void remove(const Object* aVal); };
12
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
16
by: xyz | last post by:
I have to run the simulation of a trace file around (7gb contains 116million entries)... presently i am using vector iterators to check the conditions in my program..... it is taking 2 days to...
0
by: subramanian100in | last post by:
Suppose I have vector<intc; for (int i = 0; i < 10; ++i) c.push_back(i); vector<int>::iterator it = find(c.begin(), c.end(), 5); If I do, c.insert(c.begin(), 10);
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.