c++ map sort by value and sort by key(字典的遍历)
来源:互联网 发布:淘宝卖家一直不发货 编辑:程序博客网 时间:2024/04/30 06:12
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 123123123
#define MAX_INDEX 150
using namespace std;
struct node
{
int a1,a2;
bool operator < (const node &other) const
{
if ((a1<other.a1)
||(a1==other.a1 && a2<other.a2)
)
{
return true;
}
return false;
}
};
int cmp(const pair<node,int> &x,const pair<node,int> &y)
{
return x.second < y.second;
}
void sortMapbyValue(map<node,int> &t_map,vector< pair<node,int> > &t_vec)
{
for(map<node,int>::iterator iter = t_map.begin();iter != t_map.end(); iter ++)
{
t_vec.push_back(make_pair(iter->first,iter->second));
}
sort(t_vec.begin(),t_vec.end(),cmp);
}
int main()
{
map<node,int> m_result;
vector< pair<node,int> > v_result;
node temp;
temp.a1 = 1;
temp.a2 = 2;
m_result[temp] = 1;
temp.a1 = 2;
temp.a2 = 3;
m_result[temp] = 4;
temp.a1 = 1;
temp.a2 = 3;
m_result[temp] = 10;
cout<<"sort by key :"<<endl<<endl;
for(map<node,int>::iterator iter = m_result.begin(); iter != m_result.end(); iter++)
{
printf("m_result<%d,%d>=%d\n",iter->first.a1,iter->first.a2,iter->second);
}
sortMapbyValue(m_result,v_result);
cout<<"sort by value :"<<endl<<endl;
for(int i=0; i<v_result.size(); i++)
{
cout << "m_result<" << v_result[i].first.a1 << "," << v_result[i].first.a2 << ">=" << v_result[i].second << endl;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 123123123
#define MAX_INDEX 150
using namespace std;
struct node
{
int a1,a2;
bool operator < (const node &other) const
{
if ((a1<other.a1)
||(a1==other.a1 && a2<other.a2)
)
{
return true;
}
return false;
}
};
int cmp(const pair<node,int> &x,const pair<node,int> &y)
{
return x.second < y.second;
}
void sortMapbyValue(map<node,int> &t_map,vector< pair<node,int> > &t_vec)
{
for(map<node,int>::iterator iter = t_map.begin();iter != t_map.end(); iter ++)
{
t_vec.push_back(make_pair(iter->first,iter->second));
}
sort(t_vec.begin(),t_vec.end(),cmp);
}
int main()
{
map<node,int> m_result;
vector< pair<node,int> > v_result;
node temp;
temp.a1 = 1;
temp.a2 = 2;
m_result[temp] = 1;
temp.a1 = 2;
temp.a2 = 3;
m_result[temp] = 4;
temp.a1 = 1;
temp.a2 = 3;
m_result[temp] = 10;
cout<<"sort by key :"<<endl<<endl;
for(map<node,int>::iterator iter = m_result.begin(); iter != m_result.end(); iter++)
{
printf("m_result<%d,%d>=%d\n",iter->first.a1,iter->first.a2,iter->second);
}
sortMapbyValue(m_result,v_result);
cout<<"sort by value :"<<endl<<endl;
for(int i=0; i<v_result.size(); i++)
{
cout << "m_result<" << v_result[i].first.a1 << "," << v_result[i].first.a2 << ">=" << v_result[i].second << endl;
}
}
0 0
- c++ map sort by value and sort by key(字典的遍历)
- Java8 sort map by key or value
- Map sort by value
- Sort a Map<Key, Value> by values (Java)
- Map按键排序(sort by key), 按值排序(sort by value)。
- Sort a Map<Key, Value> by values (Java) HashMap按value值排序
- Map 按值排序 (Map sort by value) - Java
- Map 按值排序 (Map sort by value) - Java
- Sort c# Dictionary by Value
- Python sort dict by value
- SORT BY C
- 遍历javascirpt object key and value help by xy
- MapReduce实现Distributed by and sort by
- Python: Sort a dictionary by value
- Sort a dictionary by value in JavaScript
- B. Little Pony and Sort by Shift
- Little Pony and Sort by Shift
- B - Little Pony and Sort by Shift
- softmax简单实现
- 服务器初学---1,C++ MySQL 连接。
- HTTP协议详解(真的很经典)
- TCP\IP协议详解
- 菜鸟好文推荐(十三)——关于代码审查的几点建议
- c++ map sort by value and sort by key(字典的遍历)
- Codeforces 574 A. Bear and Elections
- 菜鸟好文推荐(十四)——Java 常见异常及趣味解释
- android studio对从其他地方拷的代码报非法字符
- 二分查找的递归和非递归实现,二分查找的扩展
- 菜鸟好文推荐(十五)——9个基于Java的搜索引擎框架
- Context
- JVM的相关知识整理和学习 见解1
- 1044. Shopping in Mars (25)