lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 12:24 am

std::less<T> with less defined as structure.

Code: Select all

template <class _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
    bool
    operator()(const _Tp& __x, const _Tp& __y) const
    { return __x < __y; }
};
1) what's the difference between _ and __ from above?(_Tp , __x) what does it means to the compiler?
2)operator ( ). How is that operator function being called externally? (syntax wise)
std::less<T> ( &x, &y) will call the less's operator ( ) function?

Code: Select all

template<typename T, typename Comp = std::less<T>>
class PriorityQ
{
public:
  Comp comp;
  std::vector<T> v;
  unsigned n;
  
public:
  PriorityQ() : n(0) {}
  PriorityQ(Comp comp_) : comp(comp_), n(0) {}
  
  size_t size() const { return n; }
  void clear() { n = 0; }
  bool empty() { return n == 0; }
  
  void push(const T &x)
  {
    assert(v.size() >= n);
    if (v.size() == n)
      v.push_back(x);
      /**
       *  @brief  Append a single character.
       *  @param c  Character to append.
       */

    else
      v[n] = x;
    ++n;
    std::push_heap(&v[0], &v[n], comp);
  }
    
    
    /**
     *  @brief  Push an element onto a heap.
     *  @param  first  Start of heap.
     *  @param  last   End of heap + element.
     *  @ingroup heap
     *
     *  This operation pushes the element at last-1 onto the valid heap over the
     *  range [first,last-1).  After completion, [first,last) is a valid heap.
     */
3) I notice the functions of push_back and push_heap
push_back is used when vector size is =n and push_heap is used when vector size > n
what's the actual difference between these two functions from a memory stand point?


Code: Select all

for (const auto &p : top->nets())
    {
      if (p.second->is_constant())
        {
          if (p.second->constant() == Value::ONE)
            const1 = p.second;
          else
            {
              assert(p.second->constant() == Value::ZERO);
              const0 = p.second;
            }
        }
      if (const0 && const1)
        break;
    }
4)what's p.second? what's its significance? p.first, p.second and p.third?(What are their differences)

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 1:03 am

Haven't you bought that copy of the C programming book and a good C++ programming reference, yet?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 1:14 am

what's your point?
Are you telling me you know the answer above because you have a book?

yeah right!

How about just answer one question above just prove you already know the answer?

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 2:02 am

1) No difference at all. The use of underscore characters in type and variable names has no meaning to the compiler. It is just a convention. The names could have been bruce and sheila and the code would be the same.

2) std::less is a function object or functor. It could be called as follows:-

Code: Select all

int x = 0;
int y = 1;
std::less<int> intLessThan;

if (intLessThan(x, y))
{
    std::cout << "x is less than y \n";
}
However the usual purpose is to pass some operation around in the code. It is similar to passing a pointer to a function, except the functor can keep state within the object.

3) I think you need to look at the code again. push_heap is always called. The method push_back is a part of std::vector, the push_heap method is an algorithm to maintain a heap structure.

4)Again, not enough context. However, STL containers such as std::map have two values, the key and the data. When you iterate over a std::map you get the key and the date. foo.first is the key, and foo.second is the data.

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 4:26 am

@andyD, why's push_heap always called? The V.size never equal to n?

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 6:31 pm

lilzz wrote:what's your point?
Are you telling me you know the answer above because you have a book?
How about you read some books and stop asking your banal questions on here. Do your own research.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: std::less<T> and push_heap versus push_back

Mon Nov 16, 2015 7:57 pm

lilzz wrote:@andyD, why's push_heap always called? The V.size never equal to n?
Look at the code. push_heap() isn't in the preceding if-else block.
She who travels light — forgot something.

Return to “C/C++”