我有一个std :: set类,它存储了一些主数据.以下是我的设置:
std::set<TBigClass, TBigClassComparer> sSet;
class TBigClassComparer
{
public:
bool operator()(const TBigClass s1, const TBigClass s2) const
{
//comparison logic goes here
}
};
Run Code Online (Sandbox Code Playgroud)
现在我想根据TBigClass的某些字段过滤此集合中的数据,并将其存储在另一个集合中进行操作.
std::set<int>::iterator it;
for (it=sSet.begin(); it!=sSet.end(); ++it)
{
//all the records with *it.some_integer_element == 1)
//needs to be put in another set for some data manipulation
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我一个有效的方法来实现这一目标?我没有安装任何库,因此详细说明使用boost的解决方案无济于事.
更新:我正在研究C++ 98环境.
谢谢你的阅读!
我使用函数指针解决了一个问题,我有点担心解决方案并需要建议.
我在我的项目中有几个类实现了一个内存表,一个本地数据结构,它将一些数据保存在内存中.
所有函数都是私有的,因此除了类本身之外,没有其他正文可以使用它自己的函数来使用/修改数据.
最近我有一个要求,我必须在不同的模块中使用这些数据.因为我不想将私有函数设置为public,所以我在应用程序启动时将所有类的函数指针(所需函数的函数)存储在std:map中.后来我只需要通过这些指针调用函数,无论我需要修改特定类的数据.
我想知道这是否正确?使用函数指针在不同的类中调用类的私有函数?
下面的代码有点模拟我试图实现的内容:
typedef struct
{
void (*pfnDisplay)(void);
}FuncInfo;
std::map<int,FuncInfo> sFuncInfoMap;
//Stores the function pointer in a map
void RegisterFunction(void (*pfnDisplay)(void))
{
FuncInfo sFuncInfo;
sFuncInfo.pfnDisplay = pfnDisplay;
sFuncInfoMap.insert ( std::pair<int, FuncInfo>(1, sFuncInfo) );
}
class A
{
private:
static void Display(void)
{
printf("No one can access me");
}
public:
A()
{
RegisterFunction(Display);
}
};
class B
{
public:
void CallADisplay()
{
sStFuncInfoMap[1].pfnDisplay();
}
};
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
A a; //calls the constructor …Run Code Online (Sandbox Code Playgroud)