开发者

call an object's member function in std::algorithms (note: this is a different object, not in containers)

My code:

#include <algorithm>  
#include <iostream>  
#include <vector>  
using names开发者_运维问答pace std;  

class myClass {  
    public:  
        myClass() {  
            init();  
    }  
    void init() {  
        _myVector.push_back("Hello");  
        _myVector.push_back("World");  
        _myVector.push_back("Bye!");  
        for_each (_myVector.begin(), _myVector.end(), &myClass::print);  
    }  
    void print(string &myStr) {  
        cout << myStr << "." << endl;  
    }  
    private:  
        vector<string> _myVector;  
};  

int main() {  
    myClass myObj;  
    return 0;  
}  

If _myVector contained myClass objects or pointers, I could use std::mem_fun_ref or std::mem_fun. Is there any way to do the above? And yes, I do not want myClass::print to be static.


for_each (_myVector.begin(), _myVector.end(), &myClass::print);

to be replaced with

for_each (_myVector.begin(), _myVector.end(), bind1st(mem_fun(&myClass::print), this));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜