easy sorting method

I think this is a simple function to sorting something.
Is here any advices??

input data type is <int, int>.
and input data is stored at source[N].

i want to sorting input data to first column and then second column.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int FindMax(void)
{
  using data = pair<int, int>;
  priority_queue<data, vector<data>, greater<data>> pq;
  
  for(auto i=0 ; i < N ; i++)
    pq.emplace(make_pair(source[i].first, source[i].second));

  while(!pq.empty())
  {
    auto temp_data = pq.top();
    pq.pop();
    cout << temp_data.first << " " << temp_data.second << endl;
  }
}
Last edited on
And to extend, exchange pair to tuple
Since pairs and tuples are LessThanComparable if their members are LessThanComparable,
this would be simpler:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct S
{
    int first ;
    std::string second ;
    // ..
};

void foo( std::vector<S>& source )
{
    const auto cmp_first_then_second = []( const S& a, const S& b )
    { return std::make_tuple( a.first, a.second ) < std::make_tuple( b.first, b.second ) ; };

    std::sort( std::begin(source), std::end(source), cmp_first_then_second ) ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int bfs(){
    int ans = -1;
    int h, w, time;
    input_data();
    using data = tuple<int, int, int>;
    queue<data> bfs;

    bfs.emplace(make_tuple(0,0,0));

    while(!bfs.empty())
    {
        tie(h, w, time) = bfs.front();
        bfs.pop();
    
        if(h < 0 || h >= H || w < 0 || w >= W)
            continue;
        if(h == H-1 && w == W-1)
        {
            ans = time;
            break;
        }
        if(map[h][w] == '.')
        {
            map[h][w] = 'a';
            bfs.emplace(make_tuple(h-1, w, time+1));
            bfs.emplace(make_tuple(h, w+1, time+1));
            bfs.emplace(make_tuple(h+1, w, time+1));
            bfs.emplace(make_tuple(h, w-1, time+1));
        }
    }
}
Last edited on
// plus,
I make some tree depth check function. Is there any advice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int numOfDepth = 0;

void findDepthOfLeaf(multimap<int, int> contactTree, int leaf)
{
	for(auto i = contactTree.begin() ; i != contactTree.end() ; i++)
	{
		if(leaf == i->second)
		{
			findDepthOfLeaf(contactTree, i->first);
			numOfDepth++;
		}
	}
}

int main()
{
	int numOfNode, numOfEdge;
	cin >> numOfNode >> numOfEdge;
	
	multimap<int, int> contactTree;
	for(int i=0 ; i < numOfEdge ; i++)
	{
		int pNode, cNode;
		cin >> pNode >> cNode;
		
		contactTree.insert(pair<int, int>(pNode, cNode));
	}
	
	map<int, int> leafDepthPair;
	
	for(auto i : contactTree)
	{
		auto itr = contactTree.find(i.second);
		if(itr == contactTree.end())
		{
			findDepthOfLeaf(contactTree, i.second);
			leafDepthPair.insert(make_pair(i.second, numOfDepth));
			numOfDepth = 0;
		}
	}
	
	int max=0;
	int min=65535;
	for(auto i : leafDepthPair)
	{
		if(i.second > max) max = i.second;
		if(i.second < min) min = i.second;
	}
	
	if(leafDepthPair.size() == 0) min = 0;
	cout << max - min << endl;
	return 0;
}
Last edited on
It would help if you explain how a tree is represented as a std::multimap<int, int>.
Topic archived. No new replies allowed.