Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按键';的指定顺序按值对映射进行排序;优先权_C++_Algorithm_Sorting_Stdmap - Fatal编程技术网

C++ 按键';的指定顺序按值对映射进行排序;优先权

C++ 按键';的指定顺序按值对映射进行排序;优先权,c++,algorithm,sorting,stdmap,C++,Algorithm,Sorting,Stdmap,我有三张整数的地图 std::map<string,int> map1; map1["ymax"]=10; map1["ymin"]=16; map1["xval"]=10; std::map<string,int> map2; map2["ymax"]=16; map2["ymin"]=20; map2["xval"]=28; std::map<string,int> map3; map3["ymax"]=16; map3["ymin"]=20;

我有三张整数的地图

std::map<string,int> map1;

map1["ymax"]=10;
map1["ymin"]=16;
map1["xval"]=10;

std::map<string,int> map2;

map2["ymax"]=16;
map2["ymin"]=20;
map2["xval"]=28;

std::map<string,int> map3;

map3["ymax"]=16;
map3["ymin"]=20;
map3["xval"]=10;
std::map1;
map1[“ymax”]=10;
map1[“ymin”]=16;
map1[“xval”]=10;
地图地图2;
map2[“ymax”]=16;
map2[“ymin”]=20;
map2[“xval”]=28;
地图地图3;
map3[“ymax”]=16;
map3[“ymin”]=20;
map3[“xval”]=10;
一张地图包含了这些地图

std::map<string,std::map<string,int>> almap;

allmap["map1"]=map1;
allmap["map2"]=map2;
allmap["map3"]=map3; 
std::map-almap;
allmap[“map1”]=map1;
allmap[“map2”]=map2;
allmap[“map3”]=map3;
我想在内部映射中将最后一个映射排序为key
ymin
,但如果在大映射中保持相等的映射,我想将其排序为key
xval
,然后排序为key
ymax
,同样的想法


出于教育目的,正确排序到
allmap>>map1、map3、map2

std::map
要求键/值对中的键是不变的。它还要求密钥完全描述排序

allmap
的情况下,提供的键是
std::string
,这是map必须执行的全部操作,即使使用复杂的自定义比较函数也是如此

为了允许任何类型的排序,我们需要将外部名称和它们表示的映射滚动到一个关键对象中,并在此基础上进行排序

这就开始争论使用一组对象(因为现在没有关联的数据)或保留一个单独的、按自定义谓词排序的键索引

以下是后者:

#include <string>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>

struct by_keys
{
  template<class...Keys>
  by_keys(std::map<std::string, std::map<std::string, int>> const& allmap, Keys&&...keys) 
    : keys_ { std::forward<Keys>(keys)... }
  , allmap_(allmap)
  {
  }

  bool operator()(const std::string& ls, const std::string& rs) const
  {
    auto& l = allmap_.find(ls)->second; 
    auto& r = allmap_.find(rs)->second; 
    for (auto& key : keys_)
    {
      auto const& il = l.find(key);
      auto const& ir = r.find(key);
      if (il == std::end(l) && ir == std::end(r)) return false;
      if (il == std::end(l) && ir != std::end(r)) return true;
      if (il != std::end(l) && ir == std::end(r)) return false;
      if (*il < *ir) return true;
      if (*ir < *il) return false;
    }
    return false;
  }

  std::vector<std::string> keys_;
  std::map<std::string, std::map<std::string, int>> const& allmap_;
};

int main()
{
std::map<std::string,int> map1;

 map1["ymax"]=10;
 map1["ymin"]=16;
 map1["xval"]=10;

std::map<std::string,int> map2;

 map2["ymax"]=16;
 map2["ymin"]=20;
 map2["xval"]=28;

std::map<std::string,int> map3;

map3["ymax"]=16;
map3["ymin"]=20;
map3["xval"]=10;

std::map<std::string,std::map<std::string,int>> allmap;

allmap["map1"]=map1;
allmap["map2"]=map2;
allmap["map3"]=map3; 

  // ok, now lets make an index into this map

  std::vector<std::string> sorted_keys;
  for (auto& entry : allmap) { sorted_keys.push_back(entry.first); }
  std::sort(std::begin(sorted_keys), std::end(sorted_keys), 
            by_keys(allmap, "ymin", "xval", "ymax"));

  // sorted_keys should now contain the names "map1", "map3", "map2" 
}
#包括
#包括
#包括
#包括
#包括
#包括
按_键构造
{
模板
按键(std::map const和allmap、键和键)
:keys_{std::forward(键)…}
,allmap_(allmap)
{
}
布尔运算符()(常数std::string&ls,常数std::string&rs)常数
{
auto&l=allmap_u.find(ls)->秒;
auto&r=allmap_u.find(rs)->秒;
用于(自动设置关键点:关键点(&U)
{
自动常数&il=l.find(键);
自动常数&ir=r.find(键);
if(il==std::end(l)&&ir==std::end(r))返回false;
if(il==std::end(l)&&ir!=std::end(r))返回true;
如果(il!=std::end(l)和&ir==std::end(r))返回false;
if(*il<*ir)返回true;
如果(*ir<*il)返回false;
}
返回false;
}
向量键;
std::map const&allmap;
};
int main()
{
地图地图1;
map1[“ymax”]=10;
map1[“ymin”]=16;
map1[“xval”]=10;
地图地图2;
map2[“ymax”]=16;
map2[“ymin”]=20;
map2[“xval”]=28;
地图地图3;
map3[“ymax”]=16;
map3[“ymin”]=20;
map3[“xval”]=10;
std::map-allmap;
allmap[“map1”]=map1;
allmap[“map2”]=map2;
allmap[“map3”]=map3;
//好的,现在让我们为这张地图建立一个索引
std::向量排序_键;
for(auto&entry:allmap){sorted_keys.push_back(entry.first);}
std::sort(std::begin(已排序的_键),std::end(已排序的_键),
通过按键(所有地图、ymin、xval、ymax);
//排序的_键现在应该包含名称“map1”、“map3”、“map2”
}

为了教育的目的

std::map
要求键/值对中的键是不变的。它还要求密钥完全描述排序

allmap
的情况下,提供的键是
std::string
,这是map必须执行的全部操作,即使使用复杂的自定义比较函数也是如此

为了允许任何类型的排序,我们需要将外部名称和它们表示的映射滚动到一个关键对象中,并在此基础上进行排序

这就开始争论使用一组对象(因为现在没有关联的数据)或保留一个单独的、按自定义谓词排序的键索引

以下是后者:

#include <string>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>

struct by_keys
{
  template<class...Keys>
  by_keys(std::map<std::string, std::map<std::string, int>> const& allmap, Keys&&...keys) 
    : keys_ { std::forward<Keys>(keys)... }
  , allmap_(allmap)
  {
  }

  bool operator()(const std::string& ls, const std::string& rs) const
  {
    auto& l = allmap_.find(ls)->second; 
    auto& r = allmap_.find(rs)->second; 
    for (auto& key : keys_)
    {
      auto const& il = l.find(key);
      auto const& ir = r.find(key);
      if (il == std::end(l) && ir == std::end(r)) return false;
      if (il == std::end(l) && ir != std::end(r)) return true;
      if (il != std::end(l) && ir == std::end(r)) return false;
      if (*il < *ir) return true;
      if (*ir < *il) return false;
    }
    return false;
  }

  std::vector<std::string> keys_;
  std::map<std::string, std::map<std::string, int>> const& allmap_;
};

int main()
{
std::map<std::string,int> map1;

 map1["ymax"]=10;
 map1["ymin"]=16;
 map1["xval"]=10;

std::map<std::string,int> map2;

 map2["ymax"]=16;
 map2["ymin"]=20;
 map2["xval"]=28;

std::map<std::string,int> map3;

map3["ymax"]=16;
map3["ymin"]=20;
map3["xval"]=10;

std::map<std::string,std::map<std::string,int>> allmap;

allmap["map1"]=map1;
allmap["map2"]=map2;
allmap["map3"]=map3; 

  // ok, now lets make an index into this map

  std::vector<std::string> sorted_keys;
  for (auto& entry : allmap) { sorted_keys.push_back(entry.first); }
  std::sort(std::begin(sorted_keys), std::end(sorted_keys), 
            by_keys(allmap, "ymin", "xval", "ymax"));

  // sorted_keys should now contain the names "map1", "map3", "map2" 
}
#包括
#包括
#包括
#包括
#包括
#包括
按_键构造
{
模板
按键(std::map const和allmap、键和键)
:keys_{std::forward(键)…}
,allmap_(allmap)
{
}
布尔运算符()(常数std::string&ls,常数std::string&rs)常数
{
auto&l=allmap_u.find(ls)->秒;
auto&r=allmap_u.find(rs)->秒;
用于(自动设置关键点:关键点(&U)
{
自动常数&il=l.find(键);
自动常数&ir=r.find(键);
if(il==std::end(l)&&ir==std::end(r))返回false;
if(il==std::end(l)&&ir!=std::end(r))返回true;
如果(il!=std::end(l)和&ir==std::end(r))返回false;
if(*il<*ir)返回true;
如果(*ir<*il)返回false;
}
返回false;
}
向量键;
std::map const&allmap;
};
int main()
{
地图地图1;
map1[“ymax”]=10;
map1[“ymin”]=16;
map1[“xval”]=10;
地图地图2;
map2[“ymax”]=16;
map2[“ymin”]=20;
map2[“xval”]=28;
地图地图3;
map3[“ymax”]=16;
map3[“ymin”]=20;
map3[“xval”]=10;
std::map-allmap;
allmap[“map1”]=map1;
allmap[“map2”]=map2;
allmap[“map3”]=map3;
//好的,现在让我们为这张地图建立一个索引
std::向量排序_键;
for(auto&entry:allmap){sorted_keys.push_back(entry.first);}
std::sort(std::begin(已排序的_键),std::end(已排序的_键),
通过按键(所有地图、ymin、xval、ymax);
//排序的_键现在应该包含名称“map1”、“map3”、“map2”
}

创建所有地图的矢量,并按键的指定优先级顺序对其进行排序

向量v{map1,map2,map3}; std::sort(v.begin(),v.end(),[](std::map&lhs,std::map&rhs){ 回程接头(lhs[“ymax”]、lhs[“ymin”]、lhs[“xval”])< 领带(rhs[“ymax”]、rhs[“ymin”]、rhs[“xval”];) );

创建所有地图的矢量,并按键的指定优先级顺序对其进行排序

向量v{map1,map2,map3}; std::sort(v.begin(),v.end(),[](std::map&lhs,std::map&rhs){ 回程接头(lhs[“ymax”]、lhs[“ymin”]、lhs[“xval”])< 领带(rhs[“ymax”]、rhs[“ymin”]、rhs[“xval”];) );

如果保持相等映射
如果保持相等映射什么?
如果保持相等映射
如果保持相等映射什么?