std :: map使用compare参数检查第一个和第二个值(std::map checking both first and second value using compare paramete

系统教程 行业动态 更新时间:2024-06-14 17:00:14
std :: map使用compare参数检查第一个和第二个值(std::map checking both first and second value using compare parameter)

我有一个std :: map变量,它包含一个std :: string作为它的第一个参数,一个struct作为它的第二个参数。

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属性的例外:

例如。 想象两个不同的页面; usersPanel和viewImage。 这两个都有一个叫做“下载”的命令,但它们都有不同的功能。 一个下载用户日志,另一个下载图像。 (这只是一个例子)。

我希望有一个比较函数,它检查第一个参数是否与给定的输入匹配,并且entry.active为true。

我想要完成的是类似下面的代码。 这不是一个功能代码,它只是为了清楚我希望compare()参数如何工作。 检查输入是否与键类似,如果map条目的value.active为true,我希望comp()返回true。

bool comp (const std::string a, const std::string b) const { return (a == b && this.second.active); }

再一次,有几个人抱怨这段代码,所以让我这样说:那就是psuedo代码,而不是真正的代码。

我处理这个的第二种方法是为所有当前活动的菜单项都有一个临时的std :: map,但我希望我不需要添加额外的std :: map列表。 comp()会更清晰。

粘贴代码: http : //pastebin.com/1uj40Fw8

有什么建议么? ^^,

添加额外的地图持有者:

std::map<std::string, entry> entries, activeEntries;

在要显示的程序规则批准映射条目后,将其添加到activeEntries映射:

activeEntries.insert(i);

然后在activeEntries映射上使用std :: map :: find在整个菜单中找到正确的用户选择。 对于较大的菜单也更有效。

I have a std::map variable that contains a std::string as its first parameter and a struct for its second.

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;

What I need to check is the first parameters value and the second value (entry.active) in order to correctly display the menu entries. I'm using an active attribute to make sure that if there are other entries with the same std::string (this is the command to start the function entry.fptr), they won't have the same active state.

Explenation for the entry.active attribute:

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).

I would prefer to have a compare function that checks that the first parameter matches a given input, and that the entry.active is true.

What I'm trying to accomplish is something like the code below. This isn't a functional code, it's just to clearify how I want the compare() parameter to work. check if the input is similar to the key, and if the map entry has a value.active of true, I want the comp() to return true.

bool comp (const std::string a, const std::string b) const { return (a == b && this.second.active); }

Again, a few have complained about this piece of code so let me say it like this: thats psuedo code, not real code.

My second way to handle this would to have a temporary std::map for all the currently active menu entries, but I'm hoping I won't need to add an additional std::map list. The comp() would be so much cleaner.

Pastebin code: http://pastebin.com/1uj40Fw8

Any suggestions? ^^,

SOLUTION

add a extra map holder:

std::map<std::string, entry> entries, activeEntries;

After a map entry is approved by the program rules to be displayed, add it to the activeEntries map:

activeEntries.insert(i);

Then use std::map::find on the activeEntries map to locate the correct user choice, in the entire menu. Also more efficient for larger menus.

最满意答案

虽然在技术上是可行的,但是你不能在std::map使用值部分作为比较函数,因为当它在map中时不能改变键。 因此,您要么必须保留多个映射,要么可以使用std::multimap ,调用std::multimap::equal_range并在该范围内搜索活动值。

注意,您可以将密钥更改为std::pair<std::string,bool>并在那里复制活动标记,但我认为这不会对您的情况有所帮助 - 要更改密钥,您必须删除条目并重新插入修改过的密钥,这里没有多大意义。

可能更好更清洁的解决方案是将std::shared_ptr保持在不同映射中的条目取决于上下文,然后您不必重复条目,并且实际上不需要活动标志 - 只需添加到应该处于活动状态的映射。

You cannot use value part for comparison function in std::map though it is technically possible, as key must not be changed when it is inside map. So you either have to keep multiple maps or you can use std::multimap instead, call std::multimap::equal_range and search for active value inside that range.

Note, you can change key to std::pair<std::string,bool> and duplicate active flag there, but I do not think that would help in your situation - to change key you have to remove your entry and reinsert it with modified key, which does not make much sense here.

Probably better and cleaner solution would to keep std::shared_ptr to entries in different maps depend on context, then you would not have to duplicate entries and you do not actually need active flag - just add to a map where it should be active.

更多推荐

本文发布于:2023-04-18 00:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2e9584f42fc5db0c1ea9e4800e089b7b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   第二个   参数   map   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!