is the following code standard? (cleanly compiles under g++-4.0.2):
struct Asc { bool operator()(int a, int b) {return a < b;} };
struct Des { bool operator()(int a, int b) {return b > a;} };
int main() {
int arr[] = {1, 2, 3};
set<int,Asc> asc(arr, arr+3);
set<int,Des>::i terator beg = asc.begin(); //[*]
set<int,Des>::i terator end = asc.end(); //[*]
copy(beg, end, ostream_iterato r<int>(cout, "")); // prints 123
return 0;
}
note that the types of the _sets_ in both sides of assignment[*] are
different due to their different comparators (Asc/Des). nevertheless,[*] seems to be standard as the template parameters of the iterator
are 'Key' and 'Distance' (not the 'Comparator'), and so the iterator
types are equal, right? [we use user defined template 'Comparator'
parameters (Des/Asc), so they don't have standard specialization,
which means we can be sure the 'Distance' type of the two iterators
is the same; the 'Key' type (int) is also obviously the same].
thanks,
--dan
Oct 27 '05
13 2545
Greg wrote: The Comparator is a property of the container and not its iterator. In other words, the Container arranges the items in a particular order and uses the Comparator to determine that order. So any iterator that traverses the container from beginning to end will encounter each contained item in a just one specific order. Since the items can be sorted in only one order per container at a time, it would take more than just a different iterator to traverse the items in a different order.
this is well understood. indeed, I have a few different set<>s, each
holding N *handles* to *the same* N objects. the set<>s differ only
in their comparators which indeed determine the order of the sets and
effect only the inserting / deleting / searching of elements, and
*in principle*, shouldn't (technically) effect the manner in which
iteration is conducted. that is, the order of the set<> is a
derivative of the manner in which elements were inserted: any
red-black tree (or other O(logN) containers) holding elements of type
T are in principle traversed in a similar manner that is not effected
by the comparator.
the bottom line is that all that stops the following code from being
portable (and thus usable):
set<int,cmp_run time> s1; // holds N jobs ('int' = Job handle)
set<int,cmp_arr ival> s2; // holds the same N jobs
... // etc.
set<int>::itera tor beg, end;
determine_order (&beg, &end); // beg/end set to s1.begin()/s2.end()
// or s2.begin()/s2.end()
// etc. and determine_order () is O(1).
for(set<int>::i terator i=beg; i!=end; ++i)
// do whatever
is the fact that nobody thinks it's appropriate to state the above in
the standard. that is, it seems the standard should explicitly say that
type[ set<int,cmp1>:: iterator ] = type[ set<int,cmp2>:: iterator ]
as there's (seems to be) nothing to loose and a lot to gain.
my current understanding is that we agree that the alternatives (using
a base iterator class and derived iterators, or the one you mentioned
below, which I'm not even sure solves the problem) are much more complex
and pricey in terms of performance. right?
if this is indeed the case, then I'll go ahead and suggest in comp.std.c++
to consider adding the above as a requirement of STL.
thanks,
--dan Since the iterators of one container can be elements in another Container, one solution would be to declare, say, a list with the elements in one order, and then declare another list populated with the iterators (or elements) of the first list, but sorted in a different order. There would be some extra management involved in keeping the sorted lists, but some savings as well gained by essentially sharing the elements between more than one list.
Greg
Ali Çehreli wrote: How about:
if (p == RUNTIME) { for_each(s1.beg in(), /* ... */); } else if (/* ... */) { for_each(s2.beg in(), /* ... */); } // etc.
Ali
see my response to Greg from a few minutes ago: the above
is an O(k) solution (where k = number of values of p) which
involves a lot of code repetition. in contrast, my suggestion
is O(1) and eliminates all the code repetition.
in other words, to the best of my understanding, my alternative
is "polymorphi c", whereas your alternative is not.
thanks,
--dan
Dan Tsafrir wrote:
[snip] However, wouldn't it be great had such an iterator assignment been standard? I'm suggesting this because it would have allowed iteration over a collection of objects sorted according to an arbitrary criterion, without the overhead of virtual function calls:
For example, assume you have a collection of N "Job" objects sorted according to various criteria:
set<Job,cmp_run time> s1; // holds N jobs set<Job,cmp_arr ival> s2; // holds the same N jobs ... // etc.
And you want some loop to iterate through the job collection according to a criterion which is specified by a user parameter 'p'. Wouldn't being able to do the following be very beneficial in terms of performance:
set<Job>::const _iterator beg, end; if( p == RUNTIME ) { beg = s1.begin(); end = s1.end(); } else if( p == ARRIVAL ) { beg = s2.begin(); end = s2.end(); } else if( ... ) { // ... } // ...
for(set<Job>::c onst_iterator i=beg; i!=end; ++i) { // do whatever... }
Regrettably, the only alternative I can think of that will obtain the above, involves inheritance and virtual function calls that turn out to be very expensive in my application (the above loop is its kernel).
what about something like this:
#include <set>
struct Job {
int runtime;
int arrival;
};
bool cmp_runtime ( Job const & a, Job const & b ) {
return ( a.runtime < b.runtime );
}
bool cmp_arrival ( Job const & a, Job const & b ) {
return ( a.arrival < b.arrival );
}
typedef bool (*cmp_jobs) ( Job const &, Job const & );
typedef std::set< Job, cmp_jobs > JobSet;
typedef JobSet::const_i terator JobSetConstIter ;
int main ( void ) {
JobSet a ( cmp_runtime );
JobSet b ( cmp_arrival );
JobSetConstIter from = ( true ? a.begin() : b.begin() );
JobSetConstIter to = ( true ? a.end() : b.end() );
}
[snip]
Best
Kai-Uwe Bux
Kai-Uwe Bux wrote: what about something like this:
#include <set>
struct Job {
int runtime; int arrival;
};
bool cmp_runtime ( Job const & a, Job const & b ) { return ( a.runtime < b.runtime ); }
bool cmp_arrival ( Job const & a, Job const & b ) { return ( a.arrival < b.arrival ); }
typedef bool (*cmp_jobs) ( Job const &, Job const & );
typedef std::set< Job, cmp_jobs > JobSet; typedef JobSet::const_i terator JobSetConstIter ;
int main ( void ) { JobSet a ( cmp_runtime ); JobSet b ( cmp_arrival );
JobSetConstIter from = ( true ? a.begin() : b.begin() ); JobSetConstIter to = ( true ? a.end() : b.end() ); }
to the best of my understanding, as you use a pointer to the comparison
functor, you prevent the comparator code from being inlined. I think that
this might even be worse than defining and using a base-iterator-class :(
thanks,
--dan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave Benjamin |
last post by:
Hola,
I made a backport of sets.py that will run on Jython 2.1. Here is a diff
against the Python 2.3 version of sets.py. The changes were simple, but I
may have made a mistake here or there, and since the unit tests depend on
generators, it was too much trouble to try to test the module this way. I'd
appreciate any help on testing it more thoroughly. So far, everything seems
to be working fine.
The majority of the changes were due to...
|
by: Dave Benjamin |
last post by:
Hey good people,
I've been doing a lot of simultaneous Jython and CPython programming lately,
and just wanted to say, with no intended ill will toward any of the
individuals who have been generous enough to make the two languages
possible, that, well, they're kinda different.
I guess it was inevitable, but with Jython stuck at Python 2.1, it's not
really the same language as CPython is today. You still have to type "from
__future__...
|
by: Scott Smedley |
last post by:
Hi all,
I'm trying to write a special adaptor iterator for my program. I have
*almost* succeeded, though it fails under some circumstances.
See the for-loop in main().
Any pointers/help would be muchly appreciated.
Apologies for the long post - I couldn't find a shorter way to
|
by: James Stroud |
last post by:
Hello All,
I find myself in this situation from time to time: I want to compare two lists
of arbitrary objects and (1) find those unique to the first list, (2) find
those unique to the second list, (3) find those that overlap. But here is the
catch: comparison is not straight-forward. For example, I will want to
compare 2 objects based on a set of common attributes. These two objects need
not be members of the same class, etc. A function...
|
by: Lorenzo Castelli |
last post by:
This is an old problem of mine.
Basically I have an abstract base class which represents a generic iterator
over a collection of elements, and various derived classes that implement
the traversing on specific data structures.
For each iterator I want to be able to specify the four possible const
combinations, corresponding to the various void* for
pointers, with the possibility to perform conversions from non-const to
const just like...
| |
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 classes by inheriting from
it, ie.
class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};
|
by: JosAH |
last post by:
Greetings,
Introduction
This week I'll write a bit about generics (those funny angular brackets). I need
an example and decided to use sets and some of their operations. This weeks'
article discusses one set class and two interesting operations on the set:
combinations and set partitioning. First a description of the algorithms is
given and then we'll have some fun with the generic implementation of them.
|
by: subramanian100in |
last post by:
I am copying the following text as it is from Stroustrup's TC++PL 3rd
edition, page 450.
It says:
"Note that a constructor given two arguments of the same type can be a
match for more than one constructor. For example,
vector<intv(10, 50); // (size, value) or (iterator1, iterator2)?
|
by: Mr.SpOOn |
last post by:
Hi,
I need a structure to represent a set of integers. I also need to
perform on this set some basic set operations, such as adding or
removing elements, joining with other sets and checking for the
presence of specific elements.
I think that using Python sets would be the best choice, but I also
need integers to be ordered inside the set and I've just found out
that, instead, Python sets are unordered collections.
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |