I want to use functors without creating a functor class but My foundVector is showing empty even if i store a matched string in foundVector.
Also tell me is there any better way to use fucntors
I am using Visual Studio 2013. Here is my demo code:
Input:
#include<iostream>
#include<vector>
#include <string>
class Demo
{
public:
Demo(const std::string& string) :string_(string){};
void findFunctor(const std::string& string);
void operator()(const std::string &string);
void input();
private:
const std::string string_;
std::vector<std::string> storeVector;
std::vector<std::string> foundvector;
};
void Demo::input()
{
storeVector.push_back("test");
storeVector.push_back("hello");
storeVector.push_back("world");
storeVector.push_back("foo");
storeVector.push_back("hello");
}
void Demo::findFunctor(const std::string& string)
{
Demo object(string);
for (auto &elem : storeVector)
{
object(elem);
}
for (auto elem : foundvector)
{
std::cout << elem<<"\n";//Surprisingly Vector is empty and i want to know why??
}
}
void Demo::operator()(const std::string &string)
{
if (string == string_)
{
foundvector.push_back(string_);//Vector consists of two strings if matching string is found
}
}
int main()
{
Demo dObject("hello");
dObject.input();
dObject.findFunctor("hello");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire