CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    30

    stl container with 2 keys

    Hello all,

    I need a container which can handle 2 keys. Actually I need the sequence is sorted by primary key(a date time) and then second key(a businessId). Can any body help me on this?

    Thanks in advance.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: stl container with 2 keys

    You can use pretty much any container you want, you just need
    to specify a sorting function ob ject that does what you want.
    Here is a sample using vector and set ...

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <set>
    
    struct S
    {
        S() {}
        S(int d , int i) : date(d) , id(i) {}
        int date;
        int id;
    };
    
    struct SortByDateID
    {
        bool operator () (const S & lhs , const S & rhs) const
        {
            // sort by primary key first (date)
    
            if ( lhs.date < rhs.date ) return true;
            if ( lhs.date > rhs.date ) return false;
    
            // here the dates are equal , sort by secondary key (id)
    
            return lhs.id < rhs.id;
        }
    };
    
    using namespace std;
    
    int main()
    {
        vector<S> v;
    
        v.push_back( S(1,1) );
        v.push_back( S(2,3) );
        v.push_back( S(5,1) );
        v.push_back( S(2,1) );
        v.push_back( S(2,8) );
    
        sort( v.begin() , v.end() , SortByDateID() );
    
        //print sorted vector
    
        for (int i=0; i<v.size(); ++i)
        {
            cout << v[i].date << " : " << v[i].id << "\n";
        }
    
        cout << "\n\n\n";
    
        set<S,SortByDateID> s;
        
        s.insert( S(1,1) );
        s.insert( S(2,3) );
        s.insert( S(5,1) );
        s.insert( S(2,1) );
        s.insert( S(2,8) );
    
        set<S,SortByDateID>::iterator it = s.begin();
    
        while (it != s.end())
        {
            cout << it->date << " : " << it->id << "\n";
            ++it;
        }
    
        return 0;
    }

  3. #3
    Join Date
    Mar 2005
    Posts
    30

    Re: stl container with 2 keys

    Thanks Philip.
    Let me extend my requirement. I need an automatic sortable container that can sort by the primary key; I need another mechanism to sort the elements by second key. I need these can be done automatically when I insert an element into the container.
    What I am trying is to do is using a multimap to control a sequence of pointer pair. (*it)->first is a pointer to a structure that contains a date and an ID. I can provide a Less<key> function to the container like this:
    class MyLess : public binary_function<CKeyStru *, CMyValue *, bool>
    {
    public:
    bool operator()(const CKeyStru * p1, const CKeyStru * p2) const
    {
    return (_tcscmp(p1->m_bstrId, p2->m_bstrId) < 0);
    }
    };

    Now the elements are sorted by ID in the container. I need it can sorted by second key which is date time. I know that multiset can't do this but I just use this as an example. During the retrieving I can use a loop but I don't want to use a loop.

    Any idea is apprecated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured