user1647959
user1647959

Reputation: 73

Boolean operator, is there a short hand way to do this in C++?

I want to do the following:

time_heap.insert(aid.arrival(event)!=NULL);

ie, insert the returned value of aid.arrival(event) to the time_heap if it is not NULL.

This is an operation that is going to occur a lot in the main control of my program, and was hoping there is a short-hand way to do it in C++ (aside from defining my own function to handle it)

186         void insert_event(Event* value) {
187             heap.push_back(value);               // expand size of heap
188             int i = heap.size() - 1;           // set heap index to that of "value" 
189             int parent = floor((i - 1)/2);
190 
191             while (parent >= 0 && parent < heap.size()) {                //check that parent is valid
192                 if (*heap[parent] > *value) {
193                     heap[i] = heap[parent];
194                     heap[parent] = value;                                // if "value" is smaller than parent move it up in heap (swap)
195                     i = parent;                                         // set new index of "value"
196                     parent = floor((i - 1)/2);                          // set new parent of "value"
197                 }
198                 else                                                  // if parent is not larger, value satisfies min-heap condition (since all below are lower, too)
199                     break;                                                  // (i.e. we are done)
200             }
201         }

Upvotes: 0

Views: 211

Answers (4)

ipapadop
ipapadop

Reputation: 1472

Maybe something like this with my favorite ternary operator? :

#include <iostream>
#include <set>

class A {
private:
  int m_i;
public:
  A() : m_i(0) { }
  A(int i) : m_i(i) { }
  int get() const { return m_i; }
  bool operator==(const int i) const { return (m_i==i); }
  bool operator<(const A& other) const { return (m_i<other.m_i); }
};

int main() {
  std::set<A> s;
  A a;
  ( (a = A(42))==42 ? s.insert(a).second : false );
  std::cout << s.begin()->get() << std::endl;
  return 0;
}

Edit: since people don't like ternary operators, you can achieve something along the lines of what you're asking with C++11 lambdas (or Boost.Lambda):

#include <iostream>
#include <set>

class A {
private:
  int m_i;
public:
  A() : m_i(0) { }
  A(int i) : m_i(i) { }
  int get() const { return m_i; }
  bool operator==(const int i) const { return (m_i==i); }
  bool operator<(const A& other) const { return (m_i<other.m_i); }
};

int main() {
  std::set<A> s;
  A a;

  auto f = [&s] (A const& a) { if (a==42) s.insert(a); };

  f(A(42));
  f(A(43));

  std::cout << s.size() << " " << s.begin()->get() << std::endl;

  return 0;
}

Upvotes: -1

tristram shandy
tristram shandy

Reputation: 181

The expression time_heap.insert(aid.arrival(event)!=NULL); will not do what you think. The != operator is a boolean one, returning either 0 or 1. So the expression is an integral one, and I doubt your code will compile without error, since insert expects a pointer. You can probably go with something like

if ((Arrival *a = aid.arrival(event)) != NULL)
    time_heap.insert(a);

or even

if (Arrival *a = aid.arrival(event))
    time_heap.insert(a);

Personally I prefer the first option, because of the likelyhood of someone (me) missing the difference between = and == the next time the code is revised.

Upvotes: 3

daveh
daveh

Reputation: 3696

You should be able remove the compare with the NULL as NULL is considered to be a 0 value and thus false. Given "x = aid.arrival(event)" you could go with if(x){time_heap.insert(x);}

Upvotes: 0

user541686
user541686

Reputation: 210352

if (Arrival *arrival = aid.arrival(event))
    time_heap.insert(arrival);

Upvotes: 3

Related Questions