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. ATL / WTL / STL
  4. Compare two different STL list

Compare two different STL list

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorialc++
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.
  • V Offline
    V Offline
    VVVimal
    wrote on last edited by
    #1

    Hi I had two diffrent class Student { public: string name; int age; }; list _studentList1; list _studentList2; I just want to compare wheather the content of two list are same. Can any one tell me how to do that..... Below i have implemented one example for(std::list::iterator iterator1 =_studentList1.begin();studentList1.end() != iterator1; ++iterator1) { for(std::list::iterator iterator2 =_studentList2.begin();studentList2.end() != iterator2; ++iterator2) { Student* studentDetails1 = *iterator1; Student* studentDetails2 = *iterator2; if(!strcmp(studentDetails1->name ,studentDetails2->name) if(studentDetails1->age== studentDetails2->age) } } Is this is best approach to compare List ++iterator) Thanks in advance

    S 1 Reply Last reply
    0
    • V VVVimal

      Hi I had two diffrent class Student { public: string name; int age; }; list _studentList1; list _studentList2; I just want to compare wheather the content of two list are same. Can any one tell me how to do that..... Below i have implemented one example for(std::list::iterator iterator1 =_studentList1.begin();studentList1.end() != iterator1; ++iterator1) { for(std::list::iterator iterator2 =_studentList2.begin();studentList2.end() != iterator2; ++iterator2) { Student* studentDetails1 = *iterator1; Student* studentDetails2 = *iterator2; if(!strcmp(studentDetails1->name ,studentDetails2->name) if(studentDetails1->age== studentDetails2->age) } } Is this is best approach to compare List ++iterator) Thanks in advance

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      No, it's not the best way - the best way would be to define an operator== for Student, then use the operator== defined for std::list. Also - isn't the 'name' member a std::string? In which case that has an operator== Anyway - I'd do this:

      class Student
      {
      public:
      std::string name;
      int age;
      friend bool operator==(Student const& left, Student const& right)
      { return left.age==right.age && left.name==right.name; }
      };
      std::list<Student> _studentList1;
      std::list<Student> _studentList2;

      Then in code, I could do

      if (_studentList1 == _studentList2)

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      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