472,374 Members | 1,576 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,374 software developers and data experts.

STL set and its iterator.

Hi, gurus

Is it safe to do this in function?
'return &(*iterator)';
And iterator is std::set<something>::iterator

Regards,

Jul 23 '05 #1
7 4388

"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi, gurus

Is it safe to do this in function?
'return &(*iterator)';
And iterator is std::set<something>::iterator


You've got a char literal '...' and its not being assigned to anything. If
you remove the single-quotes in the statement above:

return &(*iterator);

Then no-one can answer your question since its unknown whether the address
of the derefenced object (or pointer) is local to the function.

Jul 23 '05 #2


Peter Julian wrote:
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi, gurus

Is it safe to do this in function?
'return &(*iterator)';
And iterator is std::set<something>::iterator


You've got a char literal '...' and its not being assigned to anything. If
you remove the single-quotes in the statement above:

return &(*iterator);

Then no-one can answer your question since its unknown whether the address
of the derefenced object (or pointer) is local to the function.


OK, It was my fault.
This is an example.

------------------------------------
std::set<MyClass> g_set;

void getBegin(MyClass **s)
{
std::set<MyClass>::iterator itr;
itr = g_set.begin();
*s = &(*itr); // is '*itr' temporary?
}
-------------------------------------

Jul 23 '05 #3
"Prawit Chaivong" <pr******@gmail.com> wrote in
news:11**********************@z14g2000cwz.googlegr oups.com:


Peter Julian wrote:
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
> Hi, gurus
>
> Is it safe to do this in function?
> 'return &(*iterator)';
> And iterator is std::set<something>::iterator


You've got a char literal '...' and its not being assigned to
anything. If you remove the single-quotes in the statement above:

return &(*iterator);

Then no-one can answer your question since its unknown whether the
address of the derefenced object (or pointer) is local to the
function.


OK, It was my fault.
This is an example.

------------------------------------
std::set<MyClass> g_set;

void getBegin(MyClass **s)
{
std::set<MyClass>::iterator itr;
itr = g_set.begin();
*s = &(*itr); // is '*itr' temporary?
}
-------------------------------------


IIRC: by definition *itr is a reference to the instance of the object in
the set. You then take the address of it. This pointer should be valid
until that element in the set is destroyed.
Jul 23 '05 #4
And if g_set is empty... ???

;-)

That's the fundamental problem with interfaces that return
pointers/references... What do you do when you have nothing to point or
refer to?

Jul 23 '05 #5

"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...


Peter Julian wrote:
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

<snip>
This is an example.

------------------------------------
std::set<MyClass> g_set;

void getBegin(MyClass **s)
{
std::set<MyClass>::iterator itr;
itr = g_set.begin();
*s = &(*itr); // is '*itr' temporary?
}
-------------------------------------


You are way too short on information and your knowledge on references, set
containers + iterators is rather lacking. This is making helping you very,
very difficult.

An iterator's type should always be type defined, since each STL container
supports predefined iterator types. The question you are asking refers to
*itr and whether its a temporary. When written correctly, and by using a
reference to the std::set container as a parameter to the function, *itr
returns the object thats first in the set's order of elements.

MyClass getBegin(set<MyClass>& ref_set)
{
typedef std::set<MyClass>::iterator SITER;
SITER it = ref_set.begin();
return *it;
}

A std::set container relys on a predicate
(http://en.wikipedia.org/wiki/Predicate).
When you insert an object into such a container, that container needs to
compare the insertable element to the elements already in the set. The
following will fail unless you supply an appropriate less_than operator
function to compare MyClass-type objects.

MyClass a;
MyClass b;
MyClass smallest = a < b ? a : b; // returns the lesser of the 2

By default, the std::set uses less<> to insert elements in the ordered
container. This means your MyClass class needs to have access to an
operator< for the std::set container to be able to order the elements when
inserting. Usually, the operator< is not a member of the class:

bool operator<(const MyClass& left, const MyClass& right)
{
....
}

In the case you wish to compare MyClass's private members to each other, you
need to make that operator a friend of the class.

#include <iostream>
#include <set>

class MyClass
{
int m_n;
public:
MyClass(int n) : m_n(n) { }
~MyClass() { }
int getN() { return m_n; }
// friendify op< to allow access to private members
friend bool operator<(const MyClass& left, const MyClass& right);
};

// global operator< to compare MyClass-type objects
bool operator<(const MyClass& left, const MyClass& right)
{
return left.m_n < right.m_n;
}

MyClass getBegin(std::set<MyClass>& ref_set)
{
typedef std::set<MyClass>::iterator SITER;
SITER it = ref_set.begin();
return *it;
}

int main()
{
std::set<MyClass> myset;
for (size_t i = 1; i < 10; ++i)
{
myset.insert(i); // automatically ordered
}

// using a non-member function:
MyClass first = getBegin(myset);

std::cout << "[using getBegin()]\tfirst element's value in set = ";
std::cout << first.getN();

// alternative to using a non-member function
typedef std::set<MyClass>::iterator SITER;
SITER it = myset.begin();

std::cout << "\n[using iterator]\tfirst element's value in set = ";
std::cout << (*it).getN();

return 0;
}

/* output:

[using getBegin()] first element in set = 1
[using iterator] first element in set = 1

*/
Jul 23 '05 #6

"Prawit Chaivong" <pr******@gmail.com> skrev i en meddelelse
news:11**********************@z14g2000cwz.googlegr oups.com...


Peter Julian wrote:
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
> Hi, gurus
>
> Is it safe to do this in function?
> 'return &(*iterator)';
> And iterator is std::set<something>::iterator


You've got a char literal '...' and its not being assigned to anything.
If
you remove the single-quotes in the statement above:

return &(*iterator);

Then no-one can answer your question since its unknown whether the
address
of the derefenced object (or pointer) is local to the function.


OK, It was my fault.
This is an example.

------------------------------------
std::set<MyClass> g_set;

void getBegin(MyClass **s)
{
std::set<MyClass>::iterator itr;
itr = g_set.begin();
*s = &(*itr); // is '*itr' temporary?
}
-------------------------------------

Apart from what others have mentioned, you seem to forget that *iter returns
a MyClass const&, not a MyClass&

/Peter
Jul 23 '05 #7

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:1d********************@news000.worldonline.dk ...

"Prawit Chaivong" <pr******@gmail.com> skrev i en meddelelse
news:11**********************@z14g2000cwz.googlegr oups.com...


Peter Julian wrote:
"Prawit Chaivong" <pr******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
> Hi, gurus
>
> Is it safe to do this in function?
> 'return &(*iterator)';
> And iterator is std::set<something>::iterator

You've got a char literal '...' and its not being assigned to anything.
If
you remove the single-quotes in the statement above:

return &(*iterator);

Then no-one can answer your question since its unknown whether the
address
of the derefenced object (or pointer) is local to the function.
OK, It was my fault.
This is an example.

------------------------------------
std::set<MyClass> g_set;

void getBegin(MyClass **s)
{
std::set<MyClass>::iterator itr;
itr = g_set.begin();
*s = &(*itr); // is '*itr' temporary?
}
-------------------------------------

Apart from what others have mentioned, you seem to forget that *iter

returns a MyClass const&, not a MyClass&


Thats a good point since modifying the referred_to_object could potentially
break that container's sequence.

Jul 23 '05 #8

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.