Hello,
Iterators are typically put into five different categories, namely
input iterator, output iterator, forward iterator, bidirectional
iterator and random iterator. The differences come from the
requirements each kind of iterator has to meet. Therefore, I think
the five categories are kind of conceptual thing, i.e. they are not
really C++ structs/classes etc, is this correct?
There are some functions that return iterators. For example, the
"back_inser ter" function returns an iterator when given a container.
Is the returned iterator an object of class type? For this particular
function, is the returned iterator a forward iterator only, or is it
random access iterator?
Another question is that if I implement a container, which has a
function "end()", then it should return one past the last element.
However, the one past element isn't in the container, if the "end()"
is
iterator end(){
return begin() + size();}
then the result may be a pointer pointing to some other structure. On
one hand, it looks a bit unsafe, hence I should reserve the last
element in my container (and not store any real value at that
position) to represent the one-past. On the other hand, since
deferencing an iterator to "end()" is undefined, perhaps I can just
return "begin()+size() ". Which strategy is better?
Thanks,
Jess 3 1545
On Jul 6, 12:23 pm, Jess <w...@hotmail.c omwrote:
Hello,
Iterators are typically put into five different categories, namely
input iterator, output iterator, forward iterator, bidirectional
iterator and random iterator. The differences come from the
requirements each kind of iterator has to meet. Therefore, I think
the five categories are kind of conceptual thing, i.e. they are not
really C++ structs/classes etc, is this correct?
They are C++ classes, thats why a code like
vector<intv;
vector<int>::it erator s = v.begin();
compiles
There are some functions that return iterators. For example, the
"back_inser ter" function returns an iterator when given a container.
Is the returned iterator an object of class type?
Yeah, back_inserter() returns a back_insert_ite rator for the container
for which it is invoked.
template <class Cback_insert_it erator<Cback_in serter (C& x)
For this particular
function, is the returned iterator a forward iterator only, or is it
random access iterator?
The returned iteator is a back_insert_ite rator which is an adaptor
that functions as output iterator.
Another question is that if I implement a container, which has a
function "end()", then it should return one past the last element.
However, the one past element isn't in the container, if the "end()"
is
iterator end(){
return begin() + size();}
then the result may be a pointer pointing to some other structure. On
one hand, it looks a bit unsafe, hence I should reserve the last
element in my container (and not store any real value at that
position) to represent the one-past. On the other hand, since
deferencing an iterator to "end()" is undefined, perhaps I can just
return "begin()+size() ". Which strategy is better?
C++ guarantees that you can have an iterator to one-past-last element
of a container. Dereferencing such an itertaor is not allowed however.
>
Thanks,
Jess
On 2007-07-06 09:23, Jess wrote:
Hello,
Iterators are typically put into five different categories, namely
input iterator, output iterator, forward iterator, bidirectional
iterator and random iterator. The differences come from the
requirements each kind of iterator has to meet. Therefore, I think
the five categories are kind of conceptual thing, i.e. they are not
really C++ structs/classes etc, is this correct?
Yes, and no. Input, Output, ... iterators are concepts, the actual
iterators you use are objects instantiating these concepts. So
std::list<int>: :iterator is a bidirectional iterator, but it is also a
class, with members etc. just like any other class.
Consider std::list<int>: :iterator and std::set<int>:: iterator, they are
both bidirectional iterators, since they full fill all the requirements
that comes with that concept. They are also classes that can be
instantiated just like any other class. An important point though is
that they are not related through inheritance or some other OO concept,
the only relationship they have is that they are C++ Iterators as
defined by the standard.
There are some functions that return iterators. For example, the
"back_inser ter" function returns an iterator when given a container.
Is the returned iterator an object of class type? For this particular
function, is the returned iterator a forward iterator only, or is it
random access iterator?
Output iterator.
Another question is that if I implement a container, which has a
function "end()", then it should return one past the last element.
However, the one past element isn't in the container, if the "end()"
is
iterator end(){
return begin() + size();}
That's how std::vector does it. For node-based containers another
solution have to be used.
then the result may be a pointer pointing to some other structure. On
one hand, it looks a bit unsafe, hence I should reserve the last
element in my container (and not store any real value at that
position) to represent the one-past. On the other hand, since
deferencing an iterator to "end()" is undefined, perhaps I can just
return "begin()+size() ". Which strategy is better?
Reserving an extra element should not be needed.
--
Erik Wikström
On Jul 7, 2:50 pm, Jess <w...@hotmail.c omwrote:
On Jul 7, 12:21 am, James Kanze <james.ka...@gm ail.comwrote:
[...]
However, the one past element isn't in the container, if the "end()"
is
iterator end(){
return begin() + size();}
This supposes that the iterator is random access. It won't work
with most iterators. Even with random access iterators, it's at
least as likely that begin() and end() are the primitives, and
that size() is implemented:
size_type size() const
{
return end() - begin() ;
}
then the result may be a pointer pointing to some other structure.
Thanks. Do you mean I should implement begin() and end() first as
primitives and then define size() using end() - begin()?
No. It means that some implementations do it this way. You can
do it whichever way is most convenient for you.
If I have a
container that has an underlying array as a member, I think I can
probably return the "pointer-to-arrays-last-element + 1" as the result
of "end()". Would this work?
Yes.
--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: deancoo |
last post by:
I have gotten into the habit of often using copy along with an insert
iterator. There are scenarios where I process quite a lot of data this way.
Can someone give me a general feel as to how much of a performance hit I'm
taking using this technique versus using 'copy' to copy directly into a
container with elements in place?
Thanks,
d
|
by: Pelle Beckman |
last post by:
Hi all,
I have a few newbie questions:
In function declaration what does a 'const' mean
inside the parameter list ? That it won't modify the value?
void MemberFunction(const int x);
|
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 kinds of
iterators on say a vector.
OR is it that any iterator declared on Vector is Random Iterator which
has the functionality of all the others.
|
by: Lasse Vågsæther Karlsen |
last post by:
I need to merge several sources of values into one stream of values. All
of the sources are sorted already and I need to retrieve the values from
them all in sorted order.
In other words:
s1 =
s2 =
s3 =
for value in ???(s1, s2, s3):
| |
by: ma740988 |
last post by:
typedef std::vector < std::complex < double > > complex_vec_type;
// option1
int main()
{
complex_vec_type cc ( 24000 );
complex_vec_type dd ( &cc, &cc );
}
versus
|
by: John Salerno |
last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
False
print 'hi'
hi
Thanks.
|
by: desktop |
last post by:
1)
I have this code:
std::list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
|
by: Krypto |
last post by:
Hi,
I have used Python for a couple of projects last year and I found it
extremely useful. I could write two middle size projects in 2-3 months
(part time). Right now I am a bit rusty and trying to catch up again
with Python.
I am now appearing for Job Interviews these days and I am wondering if
anybody of you appeared for a Python Interview. Can you please share
the questions you were asked. That will be great help to me.
|
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,...
|
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: 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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
| |