std::map 使用比较参数检查第一个和第二个值
std::map checking both first and second value using compare parameter
我有一个 std::map 变量,它包含一个 std::string 作为它的第一个参数和一个结构作为它的第二个参数。
struct entry {
std::string state; // works like an url eg. "/login" shows login
std::string description; // menu entry description eg. "login here"
int minAuth; // maximum user authorization level
int maxAuth; // minimum user authorization level
void (*fptr)(); //function to be called on match
bool active = false; // true if this is one of the options displayed
};
std::map<std::string, entry> entries;
我需要检查的是第一个参数值和第二个值 (entry.active),以便正确显示菜单项。
我正在使用一个活动属性来确保如果有其他条目具有相同的 std::string(这是启动函数 entry.fptr 的命令),它们将不会具有相同的活动状态。
entry.active 属性的解释:
eg. imagine two different pages; usersPanel and viewImage. Both of these
have a command called "download", however they both have different
functions. One downloads a log of users, and the other downloads an
image. (this is just an example).
我希望有一个比较函数来检查第一个参数是否与给定输入匹配,并且 entry.active 是否为真。
我想要完成的是类似于下面的代码。这不是功能代码,只是为了阐明我希望 compare() 参数如何工作。检查输入是否与键相似,如果映射条目的 value.active 为真,我希望 comp() 为 return 真。
bool comp (const std::string a, const std::string b) const
{
return (a == b && this.second.active);
}
同样,一些人抱怨这段代码,所以让我这样说:那是伪代码,不是真正的代码。
我的第二种处理方法是为所有当前活动的菜单项设置一个临时 std::map,但我希望我不需要添加额外的 std::map 列表。 comp() 会更干净。
Pastebin 代码:http://pastebin.com/1uj40Fw8
有什么建议吗? ^^,
解决方案
添加一个额外的地图夹:
std::map<std::string, entry> entries, activeEntries;
地图条目被程序规则批准显示后,将其添加到activeEntries地图:
activeEntries.insert(i);
然后在 activeEntries 地图上使用 std::map::find 在整个菜单中找到正确的用户选择。对于更大的菜单也更有效率。
虽然在技术上是可行的,但您不能在std::map
中将值部分用于比较功能,因为键在地图内部时不得更改。因此,您要么必须保留多个地图,要么可以改用 std::multimap
,调用 std::multimap::equal_range
并搜索该范围内的活动值。
请注意,您可以将密钥更改为 std::pair<std::string,bool>
并在那里复制活动标志,但我认为这对您的情况没有帮助 - 要更改密钥,您必须删除您的条目并使用修改后的密钥重新插入它, 这在这里没有多大意义。
可能更好更清洁的解决方案是根据上下文将 std::shared_ptr
保留到不同地图中的条目,这样您就不必重复条目并且实际上不需要活动标志 - 只需添加到地图中它应该是活跃的。
我有一个 std::map 变量,它包含一个 std::string 作为它的第一个参数和一个结构作为它的第二个参数。
struct entry {
std::string state; // works like an url eg. "/login" shows login
std::string description; // menu entry description eg. "login here"
int minAuth; // maximum user authorization level
int maxAuth; // minimum user authorization level
void (*fptr)(); //function to be called on match
bool active = false; // true if this is one of the options displayed
};
std::map<std::string, entry> entries;
我需要检查的是第一个参数值和第二个值 (entry.active),以便正确显示菜单项。 我正在使用一个活动属性来确保如果有其他条目具有相同的 std::string(这是启动函数 entry.fptr 的命令),它们将不会具有相同的活动状态。
entry.active 属性的解释:
eg. imagine two different pages; usersPanel and viewImage. Both of these have a command called "download", however they both have different functions. One downloads a log of users, and the other downloads an image. (this is just an example).
我希望有一个比较函数来检查第一个参数是否与给定输入匹配,并且 entry.active 是否为真。
我想要完成的是类似于下面的代码。这不是功能代码,只是为了阐明我希望 compare() 参数如何工作。检查输入是否与键相似,如果映射条目的 value.active 为真,我希望 comp() 为 return 真。
bool comp (const std::string a, const std::string b) const
{
return (a == b && this.second.active);
}
同样,一些人抱怨这段代码,所以让我这样说:那是伪代码,不是真正的代码。
我的第二种处理方法是为所有当前活动的菜单项设置一个临时 std::map,但我希望我不需要添加额外的 std::map 列表。 comp() 会更干净。
Pastebin 代码:http://pastebin.com/1uj40Fw8
有什么建议吗? ^^,
解决方案
添加一个额外的地图夹:
std::map<std::string, entry> entries, activeEntries;
地图条目被程序规则批准显示后,将其添加到activeEntries地图:
activeEntries.insert(i);
然后在 activeEntries 地图上使用 std::map::find 在整个菜单中找到正确的用户选择。对于更大的菜单也更有效率。
虽然在技术上是可行的,但您不能在std::map
中将值部分用于比较功能,因为键在地图内部时不得更改。因此,您要么必须保留多个地图,要么可以改用 std::multimap
,调用 std::multimap::equal_range
并搜索该范围内的活动值。
请注意,您可以将密钥更改为 std::pair<std::string,bool>
并在那里复制活动标志,但我认为这对您的情况没有帮助 - 要更改密钥,您必须删除您的条目并使用修改后的密钥重新插入它, 这在这里没有多大意义。
可能更好更清洁的解决方案是根据上下文将 std::shared_ptr
保留到不同地图中的条目,这样您就不必重复条目并且实际上不需要活动标志 - 只需添加到地图中它应该是活跃的。