Using remove_if

Hi everyone,
I have a map, whose pair is a string (key) and a simple class. I want to remove all items whose key string ends with a given number. I thought about using remove_if, but I'm having this error, in Visual Studio 2010 SP1:

Error 1 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility 260


Here is the code I implemented. Could you help me fix it? 'tasks' is the map I want to remove items from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool hasEnding (const string& fullString, const string& ending)
{
    if (fullString.length() >= ending.length()) {
        return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}

struct RemoveMe {
	RemoveMe (int t)
		: target(t)
	{}
	bool operator()(const pair<string, Task*>& source) {
		return hasEnding(source.first, stringf("%d", this->target));
	}
	int target;
};


void removeItems()
{
	if (tasks.empty())
		return;
	TaskIterator new_end = remove_if(tasks.begin(), tasks.end(), RemoveMe(75));	
	tasks.erase(new_end, tasks.end());
}
With maps, the key is const, so it would be pair<const string, Task*> &source

Though the whole reason map<...>::value_type exists is to avoid this problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <map>
#include <sstream>

bool has_ending( const std::string& a, const std::string& b )
{ return !b.empty() && a.size() >= b.size() && a.rfind(b) == a.size() - b.size() ; }

struct task { /* ... */ };

void remopve_items( std::map< std::string, task* >& tasks, int t ) 
{
    std::string target ;
    {
        std::ostringstream stm ;
        stm << t ;
        target = stm.str() ;
    }

    std::map< std::string, task* >::iterator iter = tasks.begin() ;
    while( iter != tasks.end() )
    {
       if( has_ending( iter->first, target ) ) iter = tasks.erase(iter) ;
       else ++iter ;
    }
}
void remopve_items(

Very nice code, though :)
Thanks JLBorges, it worked for what I need :)
Thanks for the tip, L B
Topic archived. No new replies allowed.