473,387 Members | 1,760 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.

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 dictionnaryIFStream (dictionnaryName.c_str());
istream_iterator<string> it;
for (it=istream_iterator<string> (dictionnaryIFStream);
it!=istream_iterator<string> (); ++it)
{
vector<char> sortedString ((*it).begin(), (*it).end());
sort (sortedString.begin(), sortedString.end());
dictionnary.insert (make_pair<string, string>
(string(sortedString.begin(), sortedString.end()), (*it)));
}

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

MapDictionnary::iterator j=dictionnary.begin();
MapDictionnary::iterator k;
while (true)
{
j=adjacent_find (j, dictionnary.end(), dictionnary.value_comp());
if (j==dictionnary.end())
break;

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

return 0;
}
Apr 9 '06 #1
3 4506
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 dictionnaryIFStream (dictionnaryName.c_str());
istream_iterator<string> it;
for (it=istream_iterator<string> (dictionnaryIFStream);
it!=istream_iterator<string> (); ++it)
{
vector<char> sortedString ((*it).begin(), (*it).end());
sort (sortedString.begin(), sortedString.end());
dictionnary.insert (make_pair<string, string>
(string(sortedString.begin(), sortedString.end()), (*it)));
}

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

MapDictionnary::iterator j=dictionnary.begin();
MapDictionnary::iterator k;
while (true)
{
j=adjacent_find (j, dictionnary.end(), dictionnary.value_comp());
if (j==dictionnary.end())
break;

// INCORECT FIND_IF STATEMENT !! k=find_if (j,
dictionnary.end(), dictionnary.value_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(_InputIterator, _InputIterator,
_Predicate, std::input_iterator_tag) [with _InputIterator =
std::_Rb_tree_iterator<std::pair<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(_InputIterator,
_InputIterator, _Predicate) [with _InputIterator =
std::_Rb_tree_iterator<std::pair<const std::string, std::string> >,
_Predicate = std::binder1st<std::not_equal_to<std::string> >]'
stl_tut_ref_guide_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<const
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::result_type
std::binder1st<_Operation>::operator()(const typename
_Operation::second_argument_type&) 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::result_type
std::binder1st<_Operation>::operator()(typename
_Operation::second_argument_type&) 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***********************@news.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 dictionnaryIFStream (dictionnaryName.c_str());
istream_iterator<string> it;
for (it=istream_iterator<string> (dictionnaryIFStream);
it!=istream_iterator<string> (); ++it)
{
vector<char> sortedString ((*it).begin(), (*it).end());
sort (sortedString.begin(), sortedString.end());
dictionnary.insert (make_pair<string, string>
(string(sortedString.begin(), sortedString.end()), (*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.size() << " words." << endl;

MapDictionnary::iterator j=dictionnary.begin();
MapDictionnary::iterator k;
while (true)
{
j=adjacent_find (j, dictionnary.end(), dictionnary.value_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.value_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_iterator<string> it( is );
while ( it != istream_iterator<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::iterator it = dict.begin(); it != dict.end(); ++it )
for ( set<string>::iterator it2 = it->second.begin();
it2 != it->second.end(); ++it2 )
expandedDict.insert( 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_iterator<string> it( is );
while ( it != istream_iterator<string>() ) {
string entry( *it );
sort( entry.begin(), entry.end() );
pair< MapDict::iterator, MapDict::iterator > range =
dict.equal_range( 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
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
5
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,...
18
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...
3
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 ...
10
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...
1
by: Nick Keighley | last post by:
Hi, this is cut from a larger program. <code> #include <algorithm> #include <vector>
4
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
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...
5
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...
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: 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
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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.