473,583 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find_if algorithm on multimaps

Hello,
Could someone tell me why the find_if statement applied to my multimap
dictionnary is doesn't compile? Does this algorithm doesn't work on a
multimap?

I don't understand why the adjacent_find compile and not the find_if
statement?

By the way my first intention was to write:
find_if (j, dictionnary.end (), bind1st (not_equal_to <string> (), *j));
but it doesn't compile neither for the same reason I guess.

Thanks by advance,
devel.

#include <iostream>
#include <map>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include <functional>

using namespace std;

typedef multimap<string , string, less <string> > MapDictionnary;

int
main (int argc, char **argv)
{
string dictionnaryName ;
cout << "Enter dictionnary name :";
cin >> dictionnaryName ;

MapDictionnary dictionnary;
ifstream dictionnaryIFSt ream (dictionnaryNam e.c_str());
istream_iterato r<string> it;
for (it=istream_ite rator<string> (dictionnaryIFS tream);
it!=istream_ite rator<string> (); ++it)
{
vector<char> sortedString ((*it).begin(), (*it).end());
sort (sortedString.b egin(), sortedString.en d());
dictionnary.ins ert (make_pair<stri ng, string>
(string(sortedS tring.begin(), sortedString.en d()), (*it)));
}

cout << "The dictionnary " << dictionnaryName << " contains " <<
dictionnary.siz e() << " words." << endl;

MapDictionnary: :iterator j=dictionnary.b egin();
MapDictionnary: :iterator k;
while (true)
{
j=adjacent_find (j, dictionnary.end (), dictionnary.val ue_comp());
if (j==dictionnary .end())
break;

// INCORECT FIND_IF STATEMENT !! k=find_if (j, dictionnary.end (),
dictionnary.val ue_comp());
// End of : INCORECT FIND_IF STATEMENT !!
};

return 0;
}
Apr 9 '06 #1
3 4533
devel wrote:
Hello,
Could someone tell me why the find_if statement applied to my multimap
dictionnary is doesn't compile? Does this algorithm doesn't work on a
multimap?

I don't understand why the adjacent_find compile and not the find_if
statement?

By the way my first intention was to write:
find_if (j, dictionnary.end (), bind1st (not_equal_to <string> (), *j));
but it doesn't compile neither for the same reason I guess.

Thanks by advance,
devel.

#include <iostream>
#include <map>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include <functional>

using namespace std;

typedef multimap<string , string, less <string> > MapDictionnary;

int
main (int argc, char **argv)
{
string dictionnaryName ;
cout << "Enter dictionnary name :";
cin >> dictionnaryName ;

MapDictionnary dictionnary;
ifstream dictionnaryIFSt ream (dictionnaryNam e.c_str());
istream_iterato r<string> it;
for (it=istream_ite rator<string> (dictionnaryIFS tream);
it!=istream_ite rator<string> (); ++it)
{
vector<char> sortedString ((*it).begin(), (*it).end());
sort (sortedString.b egin(), sortedString.en d());
dictionnary.ins ert (make_pair<stri ng, string>
(string(sortedS tring.begin(), sortedString.en d()), (*it)));
}

cout << "The dictionnary " << dictionnaryName << " contains " <<
dictionnary.siz e() << " words." << endl;

MapDictionnary: :iterator j=dictionnary.b egin();
MapDictionnary: :iterator k;
while (true)
{
j=adjacent_find (j, dictionnary.end (), dictionnary.val ue_comp());
if (j==dictionnary .end())
break;

// INCORECT FIND_IF STATEMENT !! k=find_if (j,
dictionnary.end (), dictionnary.val ue_comp());
// End of : INCORECT FIND_IF STATEMENT !!
};

return 0;
}


Ok, I see my mistake find_if takes an unary predicate.
My question is why
k=find_if (j, dictionnary.end (), bind1st (not_equal_to <string> (),
(*j).first));

doesn't work?

Thanks by advance,
devel.

PS: The compiler output is:

/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_algo.h:
In function `_InputIterator std::find_if(_I nputIterator, _InputIterator,
_Predicate, std::input_iter ator_tag) [with _InputIterator =
std::_Rb_tree_i terator<std::pa ir<const std::string, std::string> >,
_Predicate = std::binder1st< std::not_equal_ to<std::string> >]':
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_algo.h:336:
instantiated from `_InputIterator std::find_if(_I nputIterator,
_InputIterator, _Predicate) [with _InputIterator =
std::_Rb_tree_i terator<std::pa ir<const std::string, std::string> >,
_Predicate = std::binder1st< std::not_equal_ to<std::string> >]'
stl_tut_ref_gui de_chap13.cc:40 : instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_algo.h:187:
erreur: pas de concordance pour l'appel de «
(std::binder1st <std::not_equal _to<std::string > >) (std::pair<cons t
std::string, std::string>&) »
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_function.h: 406:
note: candidats sont: typename _Operation::res ult_type
std::binder1st< _Operation>::op erator()(const typename
_Operation::sec ond_argument_ty pe&) const [with _Operation =
std::not_equal_ to<std::string>]
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_function.h: 412:
note: typename _Operation::res ult_type
std::binder1st< _Operation>::op erator()(typena me
_Operation::sec ond_argument_ty pe&) const [with _Operation =
std::not_equal_ to<std::string>]

Apr 9 '06 #2

devel wrote:
Ok, I see my mistake find_if takes an unary predicate.
My question is why
k=find_if (j, dictionnary.end (), bind1st (not_equal_to <string> (),
(*j).first));

doesn't work?


Dereferencing your iterator yields pair<string const, string>. There is
no compare operators declared for the pair and the string needed. Roll
out your own predicate object or use boost::bind/lambda.

Apr 9 '06 #3
In article <44************ ***********@new s.free.fr>,
devel <"devel at free dot fr"> wrote:
Hello,
Could someone tell me why the find_if statement applied to my multimap
dictionnary is doesn't compile? Does this algorithm doesn't work on a
multimap?

I don't understand why the adjacent_find compile and not the find_if
statement?

By the way my first intention was to write:
find_if (j, dictionnary.end (), bind1st (not_equal_to <string> (), *j));
but it doesn't compile neither for the same reason I guess.

Thanks by advance,
devel.

#include <iostream>
#include <map>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include <functional>

using namespace std;

typedef multimap<string , string, less <string> > MapDictionnary;

int
main (int argc, char **argv)
{
string dictionnaryName ;
cout << "Enter dictionnary name :";
cin >> dictionnaryName ;

MapDictionnary dictionnary;
ifstream dictionnaryIFSt ream (dictionnaryNam e.c_str());
istream_iterato r<string> it;
for (it=istream_ite rator<string> (dictionnaryIFS tream);
it!=istream_ite rator<string> (); ++it)
{
vector<char> sortedString ((*it).begin(), (*it).end());
sort (sortedString.b egin(), sortedString.en d());
dictionnary.ins ert (make_pair<stri ng, string>
(string(sortedS tring.begin(), sortedString.en d()), (*it)));
The above is harder than it needs to be. Instead of a vector<char> use a
string. See my code below for an example.
}

cout << "The dictionnary " << dictionnaryName << " contains " <<
dictionnary.siz e() << " words." << endl;

MapDictionnary: :iterator j=dictionnary.b egin();
MapDictionnary: :iterator k;
while (true)
{
j=adjacent_find (j, dictionnary.end (), dictionnary.val ue_comp());
The only way 'j' will *not* equal 'end()' is if the same word is in the
file without any intervening words that use the same letters. For
example, if the file contained "time mite emit time mite emit", 'j'
would equal end(). Is this really what you want?

if (j==dictionnary .end())
break;

// INCORECT FIND_IF STATEMENT !! k=find_if (j, dictionnary.end (),
dictionnary.val ue_comp());
// End of : INCORECT FIND_IF STATEMENT !!
};

return 0;
}


(By this point, your function is way to long and trying to do too much.
Was this just for example purposes?)

If you are trying to stop duplicate entries then a different structure
may be called for:
----------------------------------------------------------------------
typedef map< string, set<string> > MapDict;

void extractWords( istream& is, MapDict& dict ) {
istream_iterato r<string> it( is );
while ( it != istream_iterato r<string>() ) {
string entry( *it );
sort( entry.begin(), entry.end() );
dict[entry].insert( *it );
++it;
}
}

int main() {
cout << "Enter file name: ";
string name;
cin >> name;
MapDict dict;
extractWords( ifstream( name ), dict );
// use dict here
}
----------------------------------------------------------------------
'std::set' will automatically remove duplicates, and sort the entries.

If you really want it in a multimap, then after applying the above you
can:
----------------------------------------------------------------------
int main() {
cout << "Enter file name: ";
string name;
cin >> name;
MapDict dict;
ifstream file( name );
extractWords( file, dict );
multimap< string, string > expandedDict;
for ( MapDict::iterat or it = dict.begin(); it != dict.end(); ++it )
for ( set<string>::it erator it2 = it->second.begin() ;
it2 != it->second.end() ; ++it2 )
expandedDict.in sert( make_pair( it->first, *it2 ) );
// use expandedDict
}
----------------------------------------------------------------------

If it must be a multimap from the beginning, then you should make your
check for duplicates before the insert, because trying to remove
duplicates after the fact would be really hairy.

----------------------------------------------------------------------
typedef multimap< string, string > MapDict;

// with just a little work, this can be made much more general...
// I leave that as an exorcise. :-)
struct second_is : unary_function< string, bool > {
string value;
second_is( string v ): value( v ) { }
bool operator()( MapDict::value_ type v ) const {
return v.second == value;
}
};

void extractWords( istream& is, MapDict& dict ) {
istream_iterato r<string> it( is );
while ( it != istream_iterato r<string>() ) {
string entry( *it );
sort( entry.begin(), entry.end() );
pair< MapDict::iterat or, MapDict::iterat or > range =
dict.equal_rang e( entry );
if ( find_if( range.first, range.second, second_is( *it ) ) ==
range.second )
dict.insert( make_pair( entry, *it ) );
++it;
}
}
----------------------------------------------------------------------


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 10 '06 #4

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

Similar topics

3
3373
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
5
2990
by: dave_if | last post by:
I have a vector of TCard objects, I am sorting them based on the the box field. That part works fine (finally!). I would then like to random_shuffle all cards that are in a particular box, that is, I would find the first card that is in box 3, then the first one after that which isn't in box 3, and random_shuffle that range instead of the...
18
7194
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include <bitset> using std::vector;
3
3376
by: Misiu | last post by:
Hello everybody, How to avoid using struct IsDataType for comparison for find_if algorithm but use something like that what is now commented out in the code below? Regards, Misiu template<typename T>
10
7609
by: JDT | last post by:
Hi, The following find_if works fine to me except that I want the internal individual comparison performed backward (from m+5, m+4, ..., to m). I can use reverse() to reverse the array first but that introduces overhead (a copy of the whole array). It seems that I can use a reverse iterator but the array m is not a standard STL container...
1
1481
by: Nick Keighley | last post by:
Hi, this is cut from a larger program. <code> #include <algorithm> #include <vector>
4
1879
by: want.to.be.professer | last post by:
#include <iostream> #include <list> #include <algorithm> #include <functional> using namespace std; class T { public: static bool IsEqual( int n, int m )
2
1534
by: Nikhil.S.Ketkar | last post by:
Hi, How does the == operator for multimap in STL behave ? I was under the impression that this is supposed to properly compare multimaps for equality. It seems that what it actually does is just check if each member in location L in one multimap is equal to the the member in location L in the other multimap. The documentation on...
5
7377
by: eiji.anonremail | last post by:
I would like to search in a vector depending on "a" or "a and b" without the overhead of creating a Foo object or another predicate class. Is that possible? Using a member function pointer or a global static function? Right now I have this, but I don't like it. ------------------------------Example...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8314
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...
0
8185
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5689
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...
0
5366
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
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...
1
2317
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 we have to send another system
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
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...

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.