DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010
    Posts
    119

    Sorting for equal_range()

    Hello all

    I am using 3 vectors each of which is of type struct "ABC". I am trying to find duplicates in each vector using equal_range() method. In order for equal_range() to work, the vectors must be sorted. The criteria for sorting each vector is different, therefore, I can't write a single sort method inside the struct "ABC". Is there any way to write different methods for sorting, one for each vector?

    Many Thanks

    Code:
    // sort algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    struct mystruct {
    string a;
    string b;
    string c;
    string d;
    string e;
    };
    
    Sort function-1 (does it not have to have some relevance to struct mystruct?)
    {
        On basis of variables a, b and c
    }
    
    Sort function-2 (does it not have to have some relevance to struct mystruct?)
    {
        On basis of variables a, d and e
    }
    
    Sort function-3 (does it not have to have some relevance to struct mystruct?)
    {
        On basis of variables b, c and d
    }
    
    int main () {
      
      vector< mystruct > myvector1;
      vector< mystruct >::iterator it1;
    
      sort (myvector1.begin(), myvector1.end(),Sort function-1);
    
      vector< mystruct > myvector2;
      vector< mystruct >::iterator it2;
    
      sort (myvector2.begin(), myvector2.end(),Sort function-2);
    
      vector< mystruct > myvector3;
      vector< mystruct >::iterator it3;
    
      sort (myvector3.begin(), myvector3.end(),Sort function-3);
    
      return 0;
    }

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    Use the version of std::sort() which takes a predicate, with a different predicate for each of the three vectors.
    See: http://www.codeproject.com/KB/cpp/Sort.aspx

Similar Threads

  1. Replies: 0
    Last Post: 11-08-2008, 12:53 AM
  2. Replies: 3
    Last Post: 07-29-2005, 10:00 AM
  3. Replies: 1
    Last Post: 04-05-2002, 10:18 AM
  4. Sorting for a JTable
    By Ramkumar in forum Java
    Replies: 1
    Last Post: 01-11-2001, 05:32 AM
  5. CrossBrowser DHTML table sorting problem
    By Joel Matto in forum Web
    Replies: 0
    Last Post: 05-02-2000, 03:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links