473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

finding/counting elements in a STL::map

Hi all,

As I see it there are two options for checking if for an element with
key 'x' exists in a map m....

1.) if(m.find(x)!=m.end())

or

2.) if(m.count(x))
They both take O(log N) where N is number of elements. Obviously they
both provide different things but which method should you use for just
checking if a key exists? Does it in fact matter? I have seen examples
of both
Thanks

-Mike

Jul 19 '05 #1
3 11128
"Michael H Lees" <mh*@cs.nott.ac.uk++> wrote...
As I see it there are two options for checking if for an element with
key 'x' exists in a map m....

1.) if(m.find(x)!=m.end())

or

2.) if(m.count(x))
They both take O(log N) where N is number of elements. Obviously they
both provide different things but which method should you use for just
checking if a key exists? Does it in fact matter? I have seen examples
of both


It doesn't matter. IMHO, if it's a map<>, where keys are single,
using 'count' is a bit counterintuitive (pun intended). Use whatever
you find more attractive (pun intended).

Victor
Jul 19 '05 #2
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<vf************@corp.supernews.com>...
"Michael H Lees" <mh*@cs.nott.ac.uk++> wrote...
As I see it there are two options for checking if for an element with
key 'x' exists in a map m....

1.) if(m.find(x)!=m.end())

or

2.) if(m.count(x))
They both take O(log N) where N is number of elements. Obviously they
both provide different things but which method should you use for just
checking if a key exists? Does it in fact matter? I have seen examples
of both


It doesn't matter. IMHO, if it's a map<>, where keys are single,
using 'count' is a bit counterintuitive (pun intended). Use whatever
you find more attractive (pun intended).


LOL. That's pretty cute.

However, I'd say use whatever you think other people would find
intuitive. If there is any possiblility that someone else will have to
maintain your code, take pitty on the poor slob and use find. Odds are
that using count would just confuse other people.

Dave
Jul 19 '05 #3

"Michael H Lees" <mh*@cs.nott.ac.uk++> wrote in message
news:bd**********@oyez.ccc.nottingham.ac.uk...
Hi all,

As I see it there are two options for checking if for an element with
key 'x' exists in a map m....

1.) if(m.find(x)!=m.end())

or

2.) if(m.count(x))
They both take O(log N) where N is number of elements. Obviously they
both provide different things but which method should you use for just
checking if a key exists? Does it in fact matter? I have seen examples
of both
Thanks

-Mike

I would stick to find(), as it conveys the meaning of what you are trying to
do. It will save some poor maintenance coder scratching his head over coffee
for 2 hours!
S. Armondi
Jul 19 '05 #4

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

Similar topics

2
by: yccheok | last post by:
I have an immutable object, where I do not provide implementation on =operator. However, I am facing a problem when trying to use it with stl map. stl map requires the object to have =operator...
18
by: Jan | last post by:
Hi there, i've got an STL map with something like this ( map<string, Object*> xyz; ) What happens when I call xyz.clear()? Is only the map cleared or the map and the Objects, so that the memory...
5
by: vin b | last post by:
Hi, How would I invoke a method on a reference to an STL object (STL map in this case) In this contrived example, I want to invoke the insert() method on localmap? Thanks, Vin
2
by: Allerdyce.John | last post by:
I have a a stl map which use stl pair as the key class A; type map< pair<int, int>, A> MyMap; if I have function which add entry to the map: void func(int x, int y, MyMap& map, A& a) {...
6
by: graham.macpherson | last post by:
I have 2 Suse Linux PCs which I compile my code on. Until recently they both had gcc 4.0.X on them, but I upgraded one of them to gcc 4.1.0. I have come across a very strange problem in the...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
7
by: Christian Christmann | last post by:
Hi, I've profiled one of my C++ projects and figured out that the program spends a lot of time with STL map's function "find". One of my classes possesses an STL map of the structure map<...
2
by: rockkyy | last post by:
Hi all, ok here it goes.. I am going to implement a STL MAP.. say 'mapToken'..this is a GLOBAL map. This mapToken has many nodes with its keys ranging from t1,t2,t3 ....t100 . Now in...
7
by: Alexander Kotelnikov | last post by:
Hello. I faced the same problem which arose before Message-ID: <1124162913.832263.262410@g14g2000cwa.googlegroups.com>...
2
by: Rakesh Kumar | last post by:
I am encountering the following issue with STL map iterator - wrapped within a template. ( I am wrapping it withing a template since I want to hide the map implementation from the user . At a later...
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
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,...
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...
0
tracyyun
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...
1
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...
0
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.