I wrote a little code using the map container available in the STL of C++.
The trouble I am having is that the find() member function is working
properly. When I try to find a key, if that key is not in the map, it
should return the iterator set to the address of the end of the map.
However, when I try to find a key in the map, it returns a value that is not
equal to the end of the map. I know beforehand that it will not find the
key and I expect the find() member function to return a pointer to the end
of the map.
The data is listed after the code sample below.
The code counts the occurrences of a string in a file. It stores the string
as the key in the map and the number of occurrences as the associated value.
Please help me find the error; I have included the code below:
map<char*, int> mp;
typedef pair<char*, int> pr;
map<char*, int>::iterator it;
...
/* put the string in the map */
if (mp.empty()) {
cnt = 1;
mp.insert(pr(ipStr,cnt));
}
else {
it = mp.find(ipStr);
if (it == mp.end()) { *** This is where it jumps to the 'else' ***
// add the new ip string
cnt = 1;
mp.insert(pr(ipStr,cnt));
}
else {
// increment the count
cnt = it->second;
cnt += 1;
it->second = cnt;
}
}
<><><>
Data:
218.190.48.173:4907
65.93.204.93:4989 3 1457
Jazzkt wrote: I wrote a little code using the map container available in the STL of C++. The trouble I am having is that the find() member function is working properly. When I try to find a key, if that key is not in the map, it should return the iterator set to the address of the end of the map.
However, when I try to find a key in the map, it returns a value that is not equal to the end of the map. I know beforehand that it will not find the key and I expect the find() member function to return a pointer to the end of the map.
The data is listed after the code sample below.
The code counts the occurrences of a string in a file. It stores the string as the key in the map and the number of occurrences as the associated value.
Please help me find the error; I have included the code below:
You haven't really included enough code to come to any conclusions, but I'm
suspicious of your use of char* was the key type.
When you use char* as the key type, the map will be comparing pointer values
to determine if two strings are equal. The map will not manage the strings
referenced by those pointers, neither copying them when you insert into the
map nor deleting them when you remove items from the map.
If you're inserting values from a local variable, it's entirely possible
that every pair you attempt to insert into the map has the same key value.
use std::map<std::string, int> instead.
-cd
Jazzkt wrote: I wrote a little code using the map container available in the STL of C++. The trouble I am having is that the find() member function is working properly. When I try to find a key, if that key is not in the map, it should return the iterator set to the address of the end of the map.
However, when I try to find a key in the map, it returns a value that is not equal to the end of the map. I know beforehand that it will not find the key and I expect the find() member function to return a pointer to the end of the map.
The data is listed after the code sample below.
The code counts the occurrences of a string in a file. It stores the string as the key in the map and the number of occurrences as the associated value.
Please help me find the error; I have included the code below:
map<char*, int> mp;
typedef pair<char*, int> pr;
The pr typedef is better expressed with map<char*, int>::value_type.
Actually, you should make a typedef for your map, too.
The problem is twofold. First, map<char*, int> uses less<char*> and thus
compares pointers, not the strings they point to. Second, it sounds like
you're trying to find the same char* you've inserted into the map, perhaps
because you're using the address of a local buffer. It's impossible to be
sure from the code fragments you posted. Whatever the reason, you can fix
the problem by storing std::string instead of char*.
map<char*, int>::iterator it;
...
/* put the string in the map */
if (mp.empty()) {
cnt = 1;
mp.insert(pr(ipStr,cnt));
}
else {
it = mp.find(ipStr);
if (it == mp.end()) { *** This is where it jumps to the 'else' ***
// add the new ip string
cnt = 1;
mp.insert(pr(ipStr,cnt));
}
else {
// increment the count
cnt = it->second;
cnt += 1;
it->second = cnt;
}
}
I don't want to give away too much, but see if you can replace all the above
with a single line of code no more than a dozen or so characters long.
--
Doug Harrison
Microsoft MVP - Visual C++
Doug Harrison [MVP] wrote: I don't want to give away too much, but see if you can replace all the above with a single line of code no more than a dozen or so characters long.
Life IS a test, afterall ;-)
-cd This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Khalid |
last post by:
II am allocating alot of memory for my problem model which uses stl containers
for these pointers,
will stl free the memory?
by other words what is the semantics of memory ownership in stl?
...
|
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...
|
by: Aguilar, James |
last post by:
Hey guys. A new question:
I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. ...
|
by: Harvey J Cohen, Ph. D. |
last post by:
I've been using STL for some time, but I have discovered an issue that
I'm sure has been resolved, but I'm not sure on the proper way to use STL
to solve this issue.
The issue is inheritance and...
|
by: Jon Slaughter |
last post by:
I made a simple container that contains an STL container(basicaly a wrapper)
and I want to use for each on it just like I would with an STL container.
How do I make my container STL complient so I...
| |
by: Gregory |
last post by:
I have a question about using STL containers in C++ class public
interface.
Lets say that I want to return some container from class method or
accept class method parameter as some container. For...
|
by: Daniel Marques |
last post by:
I would like to know how to get the address of a container of the STL
without using iterators. I have a class with three STL lists and a
index which points to an element of any ot the other lists,...
|
by: Sarath |
last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL
container
Unlike the similar vector<boolClass, the bitset class does not have
iterators and is not an Standard Template...
|
by: OuaisBla |
last post by:
Although STL container can't support object by reference as a template argument. To make thing worse, allocator can't support stack based allocation.
So:
std::deque<std::string const &>
is...
|
by: ademirzanetti |
last post by:
Hi there !!!
I would like to listen your opinions about inherit from a STL class
like list. For example, do you think it is a good approach if I
inherit from list to create something like...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |