// Example program
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <algorithm>
struct cmp {
bool operator()(std::string i, const std::pair<std::string,std::string>& p) const
{
return i < p.first;
}
bool operator()(const std::pair<std::string, std::string>& p, std::string i) const
{
return p.first < i;
}
};
int main(){
std::set<std::string> s1 {"--name", "--id"}; //Conditionally defined mandatory parameters
std::map<std::string, std::string> s2 { {"--name","Admin"}, {"--group","Group1"}}; //options given by user
std::set<std::string> result;
std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),
std::inserter(result, result.end()), cmp());
std::cout << *result.begin();
}
我希望输出是这样的,如果用户错过了任何一个强制参数,它应该抛出错误,同时如果用户提到了强制参数之外的任何额外参数,则再次预期会出现错误。
set_symmetry_difference 也无法使用它。
它需要能够比较您提供的任何元素组合,包括字符串与字符串以及对与对。
struct cmp {
bool operator()(const std::string &i1, const std::string &i2)
{
return i1 < i2;
}
bool operator()(const std::pair<std::string, std::string> &p1, const std::pair<std::string, std::string> &p2)
{
return p1.first < p2.first;
}
bool operator()(const std::string &i, const std::pair<std::string, std::string> &p)
{
return i < p.first;
}
bool operator()(const std::pair<std::string, std::string> &p, const std::string &i)
{
return p.first < i;
}
};
output:
--id
从文档中http://en.cppreference.com/w/cpp/algorithm/set_difference http://en.cppreference.com/w/cpp/algorithm/set_difference
比较函数的签名应等效于以下内容:
bool cmp(const Type1 &a, const Type2 &b);
签名不需要有 const &,但函数对象不能修改传递给它的对象。
类型 Type1 和 Type2 必须使得 InputIt1 和 InputIt2 类型的对象可以被取消引用,然后隐式转换为 Type1 和 Type2。
我只用一个比较函数尝试过,但它不起作用。如果我注释掉任何一个函数,它就不起作用。最后一行似乎意味着如果我测试的编译器/std 实现是正确的,则两个迭代器都必须隐式转换为两种类型。但我宁愿只提供所有四种组合。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)