Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c++

c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++testingbeta-testing
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yaaqub
    wrote on last edited by
    #1

    for the testing sort() .... what kind of the header can I use for my program. yaaqub

    N S 2 Replies Last reply
    0
    • Y yaaqub

      for the testing sort() .... what kind of the header can I use for my program. yaaqub

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      There is an example for sort in MSDN. The header file used is <algorithm>


      Nibu thomas Software Developer

      1 Reply Last reply
      0
      • Y yaaqub

        for the testing sort() .... what kind of the header can I use for my program. yaaqub

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Here is an example of how to use std::sort: ------------------------------------------- #include #include #include using namespace std; // Our data. struct Blah { Blah(int Type, const char* pName) : m_Type(Type), m_pName(pName) { } int m_Type; const char* m_pName; }; // Print out a blah. ostream& operator<<(ostream& s, const Blah& b) { s << "Type: " << b.m_Type << ", Name: \"" << b.m_pName << "\""; return s; } // Sort functors. struct SortByType { bool operator()(const Blah& l, const Blah& r) const { return l.m_Type < r.m_Type; } }; struct SortByName { bool operator()(const Blah& l, const Blah& r) const { return strcmp(l.m_pName, r.m_pName) < 0; } }; struct SortByTypeThenName { bool operator()(const Blah& l, const Blah& r) const { return SortByType()(l, r) | (!SortByType()(r, l) && SortByName()(l, r)); } }; // The data. Blah g_Blahs[] = { Blah(1, "George"), Blah(2, "Liam"), Blah(2, "Hank"), Blah(0, "Abigail"), Blah(1, "Bob"), Blah(0, "Jessica"), Blah(2, "Con"), Blah(1, "Kyle"), Blah(0, "Faye") }; int main(int argc, char* argv[]) { Blah* pBegin = &g_Blahs[0]; Blah* pEnd = &g_Blahs[sizeof(g_Blahs)/sizeof(g_Blahs[0])]; ostream_iterator oi(cout, "\n"); // Sort by type. cout << "Type:\n"; sort(pBegin, pEnd, SortByType()); copy(pBegin, pEnd, oi); cout << "\n"; // Sort by name. cout << "Name:\n"; sort(pBegin, pEnd, SortByName()); copy(pBegin, pEnd, oi); cout << "\n"; // Sort by type and name. cout << "Type and name:\n"; sort(pBegin, pEnd, SortByTypeThenName()); copy(pBegin, pEnd, oi); cout << "\n"; return 0; } Steve

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups