473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterators as associative containers' key

Hello.

A non-modifying algorithm I implemented uses two associative containers
from STL: set and map. The elements on those containers are supposed to
refer to actual elements which lie on another, bigger container.

I had the definition of my auxiliary containers based on pointers, but
considered using iterators instead. I know iterators might become
invalid upon certain operations, but as I said, the algorithm is
non-modifying. This is a snippet of what I was trying:

set<int> s;
typedef set<int>::itera tor ITER;

s.insert(1);

set<ITER> t;
t.insert(s.begi n());

In spite of my attempts, I could not get the code to work when using
iterators as keys for the associative containers. This probably has
good, obvious reasons, which are not clear to me at the moment. Would
anybody be so kind to explain that which I am missing?

Thank you,

--
Ney André de Mello Zunino

Jul 19 '05 #1
2 2384
"Ney André de Mello Zunino" <zu****@unu.edu > wrote...
A non-modifying algorithm I implemented uses two associative containers
from STL: set and map. The elements on those containers are supposed to
refer to actual elements which lie on another, bigger container.

I had the definition of my auxiliary containers based on pointers, but
considered using iterators instead. I know iterators might become
invalid upon certain operations, but as I said, the algorithm is
non-modifying. This is a snippet of what I was trying:

set<int> s;
typedef set<int>::itera tor ITER;

s.insert(1);

set<ITER> t;
t.insert(s.begi n());

In spite of my attempts, I could not get the code to work when using
iterators as keys for the associative containers. This probably has
good, obvious reasons, which are not clear to me at the moment. Would
anybody be so kind to explain that which I am missing?


Set iterators do not define operator <, which is required if no
other comparison functor is provided for the Key type. You could
try defining your own comparison for type 'ITER' and pass it to
the set<ITER, myComparisonTyp e>. The simplest I could imagine is

struct myComparisonTyp e {
bool operator()(cons t ITER& i1, const ITER& i2) const {
return std::distance(i 1, i2) < 0;
}
};

(or something like it). Try it, I used it and it seems to compile
but will it work I am not sure.

Victor
Jul 19 '05 #2
Victor Bazarov wrote:
struct myComparisonTyp e {
bool operator()(cons t ITER& i1, const ITER& i2) const {
return std::distance(i 1, i2) < 0;
}
};

(or something like it). Try it, I used it and it seems to compile
but will it work I am not sure.


You are right and your solution works great. Thank you very much for the
insight.

Regards,

--
Ney André de Mello Zunino

Jul 19 '05 #3

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

Similar topics

1
1368
by: Dave | last post by:
Hello all, Why is the term "associative" containter used to describe std::set<>, std::map<>, std::multiset<> and std::multimap<>? I always had the impression that it is related to the fact that elements in these containers are sorted. However, the (soon-to-be standard) hash-based containers are also considered associative. In these containers, element order is determined purely by the hash function and the insertion order, not by any...
4
3544
by: Merlin | last post by:
Hi, I am a C++ developer and would like to implement container classes for various types of objects. I use MS Visual C++ to compile my code. Nevertheless I like to write code that is independent from MFC and intentionally I am trying to avoid inheriting from CList and CArray classes. Recently, I became familiar with iterators and have read many articles including the GOF description on what they help to achieve. My understanding so far...
7
1706
by: Matthias Käppler | last post by:
Hi, I have the following problem: I want to store file objects in a container 8for now, it's not important how they look like). Now, on the one hand I need to be able to randomly pick files from the container as fast as possible. So far I have been using std::map to store the files, whereas the key was a unique file ID. That given, I could pick random files from the container in O(logn). On the other hand, I need functionality to sort...
3
2929
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.
8
1996
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a function where I with the help of iterators want to search through the container and remove a certain object in the container. Here is part of the code in that function:
4
2694
by: kalita | last post by:
Hi All, typedef std::list<int> Cont; void f(Cont &c1, Cont &c2) { Cont::iterator it = c1.begin(); c1.swap(c2); it == c2.begin(); // is this ill formed? }
18
2125
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);
5
1883
by: desktop | last post by:
set, map, multiset and multimap are all associative containers. But what does the word "associative" have to do with these 4 containers?
1
1249
by: anonymous | last post by:
I have a function that returns a map<string, vector<int. Inside the function I load the map with this instruction: loc_map.push_back(1); I would like to remove the output data structure and to use a template function with iterators. My idea is to use an iterator in this way:
0
9720
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
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, 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...
0
10372
jinu1996
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...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
agi2029
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...
1
7650
isladogs
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...
0
5546
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...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.