Notices
Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By MagiMaster
  • 1 Post By Drumstig

Thread: C++ language, What the shortest way to compare two structures?

  1. #1 C++ language, What the shortest way to compare two structures? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    i know structures cant be compared wholly, like:

    Code:
    asd p1, p2
    if(p1==p2){cout<<"compared";}
    it can only be compared like

    Code:
    persondetails p1, p2;
    if (p1.name == p2.name &&
    p1.gender == p2.gender &&
    p1.age == p2.age &&
    p1.height == p2.height &&
    p1.weight == p2.weight &&
    p1.occupation == p2.occupation 
    ... many more......
    ){cout<<"p1 is p2";}
    what if i have many many variables in the structure, maybe hundreds, is there a special function to compare them, that i can also use on any two structure objects?(assuming they are from the same "struct{};")


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    You could treat the pointers to the two structures as just pointers to memory and use memcmp().
    memcmp - C++ Reference

    If you are just doing it to save writing out the comparisons in full then, personally, I would fail you for doing it.

    Another possibility would be to keep a checksum/hashcode for all the data in the object. You can update this whenever you modify any of the members of the structure (assuming you are doing it properly and making all accesses via get/put type functions). Then all you need to do is compare the hash codes for two data structures. If you use standard data structures, they may already include support this (but I'm not sure; my C++ is fairly rusty now).


    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  4. #3  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    Write a comparison function and you'll only have to code it once. Then you can focus on doing that once right.
    Strange likes this.
    Reply With Quote  
     

  5. #4  
    Forum Freshman
    Join Date
    Aug 2013
    Posts
    18
    i think operator overloading is what you are looking for

    a struct is basicly a public class, it works the same as a class

    its a method you have to overload like this

    Code:
    bool operator== (const asd & Rvalue){..}
    Strange likes this.
    Reply With Quote  
     

  6. #5  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    is there any operator that can be used to compare any two same structure-types?
    an operator that can compare any two structures (assuming any two structures' insides{...} have the same variable types)?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  7. #6  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    Quote Originally Posted by RamenNoodles View Post
    is there any operator that can be used to compare any two same structure-types?
    an operator that can compare any two structures (assuming any two structures' insides{...} have the same variable types)?
    No, because the operator would need to know the internal details of each structure and how to compare the contents. Possibly recursively. Or maybe not, depending on the definition of equality (and, if relevant, ordering operators).

    You need to define your own, as Drumstig suggests.
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  8. #7  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    920
    is there a way to know if a variable is a double, float, or integer in an "if" statement?

    e.g.
    Code:
    double myvar;
    if(myvar==a double variable){
    cout<<"Its a double";
    }
    else if(myvar== an integer variable){
    cout<<"Its an integer";
    }
    any ways to find out
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  9. #8  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,670
    You need to look at the typeof or typeid operators.
    http://publib.boulder.ibm.com/infoce...d_operator.htm
    type_info - C++ Reference
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  10. #9 programming 
    Forum Freshman
    Join Date
    Jan 2013
    Posts
    7
    You can use this structure, code..........
    if(p1==p2)
    {
    cout<<"compare";
    }
    Reply With Quote  
     

  11. #10  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    Have you actually tried using that with a complex structure? Hint: it won't (always) work like you expect.

    In more detail, and IIRC, that only works with POD structures (plain old data, an actual technical term in C/C++).
    Reply With Quote  
     

Similar Threads

  1. How to compare two objects?
    By Natalia_RU in forum Mathematics
    Replies: 2
    Last Post: July 6th, 2013, 10:26 PM
  2. Pain language, a universal language for the world!
    By Japith in forum General Discussion
    Replies: 8
    Last Post: July 18th, 2012, 05:00 AM
  3. shortest path
    By AlexP in forum Mathematics
    Replies: 8
    Last Post: March 12th, 2008, 08:47 PM
  4. shortest path optimizations
    By warnockm in forum Computer Science
    Replies: 1
    Last Post: November 28th, 2007, 06:34 AM
  5. Is this the Shortest disproof in history
    By dreamsleeper in forum Mathematics
    Replies: 6
    Last Post: September 16th, 2007, 08:16 PM
Bookmarks
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
  •