-
Map and Functor :: STL
Hi.
How do you define a functor that handles elements in an STL map? For example:
Code:
std::map<int, char *> testMap;
char *testChar = new char[10];
_strcpy(testChar, "November");
testMap.insert(std::pair<int, char *>(0, testChar));
...
// Now I want to deallocate all values in testMap.
std::for_each(testMap.begin(), testMap.end(), DeleteValue());
How do you define DeleteValue functor that handles elements in an STL map?
Thanks,
Kuphryn
-
use string :D
if there is a reason you're not using string i'd like to hear it..
if you have some legitimate need to use char:
search your local STL reference for map::first and map::second, IIRC these are the names of the keys and values.
-
The question is about functors, not char *.
std::map<int, std::string *> testMap;
std::string *pText = new std::string;
...
How do you define a functor that handles elements from a map?
Kuphryn
-
Well, without knowing much about the STL (and not caring either), it's hard to say exactly how it may be implemented. Basically, to build a functor, you need:
- a structure to hold a pointer to an object, and a pointer to the function it will call.
- a function to assign the above to a given structure.
- for added genericity, derive the functor from a non-templated base-class.
Code:
class functor_container { public:
virtual void invoke() = 0;
};
template <class type>
class functor : public functor_container { public:
functor(type* instance, void (type::* function)(void)){
object = instance;
invocation = function;
}
void invoke(){
(*object.*invocation)();
}
private:
type * object;
void (type::*invocation)();
};
class test {
public:
String text;
test(char * this_text)
:text(this_text)
{ /* */ }
void print( ){
cout << text << endl;
getch();
}
};
int main(){
test attempt("Hello from a functor...");
functor <test> test_functor(&attempt, &test::print);
system("pause");
test_functor.invoke();
return 0;
}
-
And gasp.. an actual answer to the question, without a rant about the STL :).
Code:
#include <iostream>
#include <map>
#include <algorithm>
typedef std::pair<const int, int> mapitem;
void printitem(mapitem& m) {
std::cout << m.first << ' ' << m.second << std::endl;
}
int main() {
std::map<int, int> squares;
for (int i = 0; i < 10; ++i) {
squares[i] = i*i;
}
std::for_each(squares.begin(), squares.end(), printitem);
return 0;
}
-
:D Yes, yours is quite relevant, isn't it? Good example, too.
-
Sorry guys but neither of those are functors.
You see a functor is a class that has an overloaded operator () and neither of yours have.
You need to overload operator () to do a map.erase() kuphryn erasing the mapitem pointed to by a passed iterator.
-
Thanks everyone.
Here is one answer by Joaquín M López Muñoz of CodeProject.
Code:
class DeleteValue : public std::unary_function<std::pair<int const,char *>&, void>
{
public:
void operator()(std::pair<int const,char *> &pairObject)
{
delete [] pairObject.second;
}
};
Kuphryn
-
I always thought of a functor as anything that acts like a function... they do too.
http://www.codeproject.com/csharp/cs_functors.asp
"One of the tools that an STL-programmer must learn to master is the functor concept. A functor is any valid expression that can be syntactically written as a function call. Those expressions can be functions, function pointers and operator() overloading."