STL remove_if function

P

Paavo K

Hi, can anyone tell what is wrong with following code :

//..Code

class removeTest
{
public:
bool operator()(map<int, string>::value_type &d) const
{
// Just returning false to simplify code..
return false;
}
};

void testFunction()
{
map<int, string> *m = new map<int, string>;

if(m->empty() == false)
{

m->erase(
remove_if(m->begin(),
m->end(),
removeTest()),
m->>end()
);

}
else cout<<"\n Nothing to remove \n";
}


Visual C++ compiler return this message :

c:\microsoft visual studio\vc98\include\algorithm(317) :
error C2582: 'std::pair<int const ,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >' : 'operator ='
function is unavailable

Thanks in advance!
 
D

David Harmon

map<int, string> *m = new map<int, string>; ....
remove_if(m->begin(),
m->end(),
removeTest()),
m->>end()
);

You cannot use std::remove_if against a std::map. remove_if reorders
elements (perhaps surprisingly, it does not really remove anything) and
elements of a map cannot be reordered.
Visual C++ compiler return this message :

c:\microsoft visual studio\vc98\include\algorithm(317) :
error C2582: 'std::pair<int const ,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >' : 'operator ='
function is unavailable

Notice the "const" in the above message. You cannot assign to a
std::pair with a const member.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top