[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

compare a struct with standard list

Hi Experts,

I have the following struct in c++ :

typedef struct
{
    std::string     type;
    std::string     name;
    std::list<std::string> contacts
    bool            connected;
} clientDetail;

I have 2 questions :
1- how to initialize the struct to null;

2 - I need to create a method that compares two client Detail structs, how should I do that ? Do I need to allocate dynamic memory for the list ?

Thanks,

B.L.
0
bachra04
Asked:
bachra04
  • 2
  • 2
  • 2
2 Solutions
 
peprCommented:
Actually, there is no one-and-the-only "null" for that case. Also NULL has a bit low-level meaning (basically "put all related memory bytes to zero").

In your specific example, the string type is automatically initialized to an empty string, the list to an empty list. The only problem is with bool that is a kind of lower-level object. It should have one of false/true values. You can fill it with zero somehow to initialize it to false. However, it is better to initialize it explicitly to false instead. Using the C++11 standard, you can modify your last element definition to bool connected { false }; (and do not forget to add the semicolon to the previous line).

A struct is basically treated as class with public elements. You should probably convert it to the class as it does not help anything to keep it struct.

For the comparison, the two approaches are usually used: 1) write a function that takes the two compared objects (usually constant references to them), 2) write a method of the same class that compares itself with the passed argument. Understanding the second approach, you can add syntactic sugar and overload the wanted operator.
0
 
jkrCommented:
Regarding the conmparison, you could just create an operator for that purpose, i.e.

#include <list>
#include <string>

typedef struct
{
    std::string     type;
    std::string     name;
    std::list<std::string> contacts;
    bool            connected;
} clientDetail;

bool operator==(const clientDetail& l, const clientDetail& r) {

  if (l.type != r.type) return false;

  if (l.name != r.name) return false;

  if (l.connected != r.connected) return false;

  // compare contact list. You can skip that if this is not required
  if (l.contacts.size() != r.contacts.size()) return false;

  std::list<std::string>::const_iterator i, j;

  i = l.contacts.begin();
  j = r.contacts.begin();

  while( i != l.contacts.end() && j != r.contacts.end()) {

    if (*i != *j) return false;

    ++i; ++j;
  }

  return true;
}

void main () {

  clientDetail a, b;

  // ...

  bool c = a == b; 
}

Open in new window

0
 
peprCommented:
To add, it is possible to compare the lists using std::equal standard algorithm. This way, the above code part for the lists could be rewritten like this:
    if (l.contacts.size() == r.contacts.size())
        return std::equal (l.contacts.begin(), l.contacts.end(), r.contacts.begin());
    else
        return false;

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bachra04Author Commented:
I assume it will be the same for overloading the assignment operator ?
0
 
bachra04Author Commented:
I mean do  I need to overload assignment operator or the default = operator works fine between two standard lists.
0
 
jkrCommented:
Um, nor sure what you mean - overloading the assignment opeator is only nessecary for assignments, not for comparisons. But, in  a bigger picture, it'd be similar.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now