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

STL map search

Hi forum,

If you follow the output of the current code snipet
(is supposed to be compilable with VC6 or gcc)
you'll see that the search stops after the second key...
So what is wrong with my code ?

#ifdef _MSC_VER
#include <functional>
#else
#include <bits/stl_function.h>
#endif

#include <iostream>
#include <map>

class CCharPComp : public std::binary_function < const char *, const char *, bool > {
public :
bool operator()( const char *arg1, const char * arg2) const {
std::cout << "First str : " << arg1 << std::endl
<< "Second str : " << arg2 << std::endl
<< "Result : " << strcmp( arg1, arg2 ) << std::endl;
return strcmp( arg1, arg2 );
}
};

std::map < const char *, const char *, CCharPComp > trMap;
std::map < const char *, const char *, CCharPComp >::const_iterator mapIter;

void justPrint(const char *actKey){

mapIter = trMap.find(actKey);

if ( mapIter == trMap.end() )
std::cout << "Not Found" << std::endl;
else
std::cout << "Found : " << mapIter->second << std::endl;
}

int main(void) {
const char * strs[8] = { "firstKey", "firstVal", "secondKey", "secondVal", "thirdKey", "thirdVal", "fourthKey", "fourthVal"};

trMap.insert(std::pair<const char *, const char*>(strs[0], strs[1]));
trMap.insert(std::pair<const char *, const char*>(strs[2], strs[3]));
trMap.insert(std::pair<const char *, const char*>(strs[4], strs[5]));
trMap.insert(std::pair<const char *, const char*>(strs[6], strs[7]));

std::cout << std::endl << "Start searching" << std::endl;

//justPrint("secondKey");

justPrint("fourthKey");
}
Sep 7 '05 #1
3 5584
Thomas Fazekas wrote:
Hi forum,

If you follow the output of the current code snipet
(is supposed to be compilable with VC6 or gcc)
you'll see that the search stops after the second key...
So what is wrong with my code ?

#ifdef _MSC_VER
#include <functional>
#else
#include <bits/stl_function.h>
#endif
Why this?
#include <functional> should do it.

#include <iostream>
#include <map>

class CCharPComp : public std::binary_function < const char *, const char *, bool > {
public :
bool operator()( const char *arg1, const char * arg2) const {
std::cout << "First str : " << arg1 << std::endl
<< "Second str : " << arg2 << std::endl
<< "Result : " << strcmp( arg1, arg2 ) << std::endl;
return strcmp( arg1, arg2 );
return strcmp( arg1, arg2 ) < 0;
[SNIP]


Worked for me.

Stefan
Sep 7 '05 #2
Thomas Fazekas wrote:

class CCharPComp : public std::binary_function < const char *, const char *, bool > {
public :
bool operator()( const char *arg1, const char * arg2) const {
std::cout << "First str : " << arg1 << std::endl
<< "Second str : " << arg2 << std::endl
<< "Result : " << strcmp( arg1, arg2 ) << std::endl;
return strcmp( arg1, arg2 );
}
};
What you need to implement is a binary predicate, that tells if one argument
*is less* then the other. That means:

return value meaning
------------------------------------------
true arg1 *is less then* arg2
false arg1 *is not less then* arg2

now look at the return values of strcmp

return value meaning
------------------------------------------- 0 arg1 *is greater then* arg2

== 0 arg1 *is equal to* arg2
< 0 arg1 *is less then* arg2

Now combine that with the usual C++ convention that everything
not 0 is regarded as true and you get:
your operator does not implement the required *is less then* functionality,
but implements *is equal to*.

a simple

return strcmp( arg1, arg2 ) < 0;

cures this one.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 7 '05 #3
Thnaks a lot, it works just fine now :)

Thomas Fazekas wrote:
Hi forum,

If you follow the output of the current code snipet
(is supposed to be compilable with VC6 or gcc)
you'll see that the search stops after the second key...
So what is wrong with my code ?

#ifdef _MSC_VER
#include <functional>
#else
#include <bits/stl_function.h>
#endif

#include <iostream>
#include <map>

class CCharPComp : public std::binary_function < const char *, const char
*, bool > {
public :
bool operator()( const char *arg1, const char * arg2)
const {
std::cout << "First str : " << arg1 << std::endl
<< "Second str : " << arg2 <<
std::endl << "Result : " <<
strcmp( arg1, arg2 ) << std::endl;
return strcmp( arg1, arg2 );
}
};

std::map < const char *, const char *, CCharPComp > trMap;
std::map < const char *, const char *, CCharPComp >::const_iterator
mapIter;

void justPrint(const char *actKey){

mapIter = trMap.find(actKey);

if ( mapIter == trMap.end() )
std::cout << "Not Found" << std::endl;
else
std::cout << "Found : " << mapIter->second << std::endl;
}

int main(void) {
const char * strs[8] = { "firstKey", "firstVal", "secondKey",
"secondVal", "thirdKey", "thirdVal", "fourthKey", "fourthVal"};

trMap.insert(std::pair<const char *, const char*>(strs[0],
strs[1])); trMap.insert(std::pair<const char *, const
char*>(strs[2], strs[3])); trMap.insert(std::pair<const char *,
const char*>(strs[4], strs[5])); trMap.insert(std::pair<const char
*, const char*>(strs[6], strs[7]));

std::cout << std::endl << "Start searching" << std::endl;

//justPrint("secondKey");

justPrint("fourthKey");
}


Sep 8 '05 #4

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

Similar topics

0
by: R. Rajesh Jeba Anbiah | last post by:
Q: Is PHP search engine friendly? Q: Will search engine spiders crawl my PHP pages? A: Spiders should crawl anything provided they're accessible. Since, nowadays most of the websites are been...
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
5
by: George | last post by:
Hi, Anyone has the background for explaining? I have made a search on my name and I have got a link to another search engine. The link's title was the search phrase for the other search engine...
3
by: Alastair | last post by:
Hello guys, I've been building a search facility for an intranet site I'm part of developing and we've been building a search engine using Index Server. It mostly works, however there have been...
39
by: Noticedtrends | last post by:
Can inference search-engines narrow-down the number of often irrelevant results, by using specific keywords; for the purpose of discerning emerging social & business trends? For example, if...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
0
by: passion | last post by:
"Specialized Search Engines" along with Google Search Capability (2 in 1): http://specialized-search-engines.blogspot.com/ Billions of websites are available on the web and plenty of extremely...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...

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.