473,385 Members | 2,013 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,385 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 2317
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.