如何创建迭代器来解决这个问题?

时间:2011-04-23 10:40:12

标签: c++ search boost map set

所以我们有3张地图。 args,header和params地图。它们是字符串到字符串的映射 我们还有set<settings> Settings_Set其中:

struct settings
{
    string name;  
    set<string> args;
    set<string> headers;
    set<string> params;
};

我们希望在Settings_Set的所有项目中比较我们所有的map-&gt;第一个字符串和所有集合。比较之后或我们想知道

  • 如果在我们的地图中找到了多个我们在Settings_Set中的sttings
  • 如果我们的地图中只找到一个设置(并输出其名称)
  • 如果我们在地图中找不到任何设置

那么如何创造这样的东西?

1 个答案:

答案 0 :(得分:3)

你根本不需要提升。此外,即使您将其称为“地图”,我也看不到std::map的任何实例。我只看到套装。

这不是本身的解决方案,而是更多的解决方法。您可以使用:

循环遍历set<settings> Settings_Set的元素
set<settings>::iterator ite = Settings_Set.begin();

while(ite != Settings_Set.end())
{
// do what you need to do in here
// with the corresponding ite->name, ite->args, ite->headers, and ite->params

// Also, you can see if something is in a set by using:
// set<string>::iterator ites = ite->params.find(std::string("some string"));
// which will put the appropriate iterator into ites if one is found
// or ite->params.end() if one is not found

++ite;
}

您应该可以将其与std::set上的标准参考之一拼凑起来:STL set reference

(注意我没有故意使用const_iterator,因为我假设您可能正在更改值,正如您在帖子中所说的那样。)