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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting a vector with structs

Sorting a vector with structs

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsalgorithmshelp
2 Posts 2 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.
  • W Offline
    W Offline
    whofmans
    wrote on last edited by
    #1

    Hello, I would like to sort a vector(vlines), this is a vector with struct's(line). I want to sort the vector on name and on length. struct line{ CString name; double length; }; vector vlines; I know there is a function sort and that it uses a predikaat function. std::sort(vlines.begin(), vlines.end(), length()); std::sort(vlines.begin(), vlines.end(), name()); But how can i code the predikaat function's? or template's??:confused: I hope someone can help me? Thanks

    J 1 Reply Last reply
    0
    • W whofmans

      Hello, I would like to sort a vector(vlines), this is a vector with struct's(line). I want to sort the vector on name and on length. struct line{ CString name; double length; }; vector vlines; I know there is a function sort and that it uses a predikaat function. std::sort(vlines.begin(), vlines.end(), length()); std::sort(vlines.begin(), vlines.end(), name()); But how can i code the predikaat function's? or template's??:confused: I hope someone can help me? Thanks

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Those predicates are anything (functions, objects) that can be called with the same syntax as a function. So, even a function can do; for instance:

      inline bool compare_line_by_length(const line&x,const line& y)
      {
      return x.name<y.name;
      }

      can be fed to std::sort like this:

      std::sort(vlines.begin(), vlines.end(), compare_line_by_length);

      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      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