Pagination


1,28,310.6,SF;
4,5,204.1,SF;
20,7,203.2,Oakland;
6,8,202.2,SF;
7,20,180.1,SF;
6,10,199.1,SF;
1,16,190.4,SF;
2,18,161.2,SF;
3,76,146.2,SF;
6,29,185.2,SF;
6,21,162.1,SF;
2,30,149.1,SF;
2,14,141.1,San Jose


 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
#include <list>
#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

namespace airbnb
{
    class Pagination
    {
    public:
        Pagination() {}
        ~Pagination() {}

        void pagination(list<string>& results, int pagesize)
        {
            unordered_set<string> visited;
            list<string> ::iterator itr = results.begin();
            int item = 0;

            while (!results.empty())
            {
                string id = (*itr).substr(0, (*itr).find_first_of(','));
                if (visited.find(id) == visited.end())
                {
                    visited.insert(id);
                    item++;
                    cout << (*itr) << endl;

                    itr = results.erase(itr);

                    if (item == pagesize)
                    {
                        visited.clear();
                        item = 0;
                        itr = results.begin();
                        cout << "---- page end ----" << endl;
                        continue;
                    }
                }
                else
                {
                    itr++;
                }

                if (itr == results.end()) {
                    itr = results.begin();
                    visited.clear();
                }
            }
        }
    };
}

Comments

Popular posts from this blog

Unique Binary Search Trees

Coin in Line