473,387 Members | 1,798 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,387 software developers and data experts.

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>::iterator ITER;

s.insert(1);

set<ITER> t;
t.insert(s.begin());

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 2362
"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>::iterator ITER;

s.insert(1);

set<ITER> t;
t.insert(s.begin());

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, myComparisonType>. The simplest I could imagine is

struct myComparisonType {
bool operator()(const ITER& i1, const ITER& i2) const {
return std::distance(i1, 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 myComparisonType {
bool operator()(const ITER& i1, const ITER& i2) const {
return std::distance(i1, 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
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...
4
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...
7
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...
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...
8
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...
4
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
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
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
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.