⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sjzalgo.h

📁 ALGAE是一个快速创建算法演示的框架。目前支持的算法实现语言包括java和c
💻 H
字号:
#ifndef STD_ALGO_H
#define STD_ALGO_H
//
//  Provides a few of the algorithm fragments that would normally be
//  found in the std <algo.h>
//

template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last,
                     OutputIterator result)
{
  while (!(first == last))
    {
      *result = *first;
      ++first;
      ++result;
    }
  return result;
}


template <class InputIterator1, class InputIterator2>
int equal (InputIterator1 first1, InputIterator1 last1,
           InputIterator2 first2)
{
  while (!(first1 == last1) && (*first1 == *first2))
    {
      ++first1;
      ++first2;
    }
  return (first1 == last1);
}

template <class OutputIterator, class T>
void fill (OutputIterator first, OutputIterator last, const T& x)
{
  while (!(first == last))
    {
      *first = x;
      ++first;
    }
}

template <class OutputIterator, class Int, class T>
void fill_n (OutputIterator first, Int n, const T& x)
{
  while (n > 0)
    {
      *first = x;
      ++first;
      --n;
    }
}

template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& x)
{
  while (!(first == last) && !(*first == x))
    {
      ++first;
    }
  return first;
}

template <class InputIterator, class Predicate>
InputIterator find_if (InputIterator first, InputIterator last, Predicate p)
{
  while (!(first == last) && !p(*first))
    {
      ++first;
    }
  return first;
}

template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f)
{
  while (!(first == last))
    {
      f(*first);
      ++first;
    }
  return f;
}

#ifndef __MINMAX_DEFINED
// __MINMAX_DEFINED is used by Borland Turbo C++, 4.5
#define __MINMAX_DEFINED

#ifndef max
template <class T>
inline const T& max (T& x, T& y)
{
 return (x < y) ? y : x;
}
#endif

#ifndef min
template <class T>
inline const T& min (T& x, T& y)
{
 return (x < y) ? x : y;
}
#endif

#endif

template <class T>
void swap (T& x, T& y)
{
  T temp = x;
  x = y;
  y = temp;
}


template <class RandomIterator,
                class Compare>
void pop_heap (RandomIterator first,
                         RandomIterator last,
                         Compare comp)
{
  swap (*first, *last);
  RandomIterator parent = first;
  while ((parent - first) < (last - first))
   {
    RandomIterator child1 =
            2 * (parent - first + 1);
    if ((child1 - first) < (last - first))
       {
        RandomIterator child2 =  child1 + 1;
        if ((child2 - first) < (last - first)
           && (*child2 < *child1))
          child1 = child2;
        if (*child1 < *parent)
           {
             swap (*child1, *parent);
             parent = child1;
           }
        else
           parent = last;
       }
    else
        parent = last;
   }
}



template <class RandomIterator,
                class Compare>
void push_heap (RandomIterator first,
                           RandomIterator last,
                           Compare comp)
{
  RandomIterator parent;
  parent = first + (last - first - 1)/2;
  while (last != first && *last < *parent)
     {
      swap(*last, *parent);
      last = parent;
      parent = first + (last - first - 1)/2;
     }
}



////////////////////////////////////

template <class Arg, class Result>
struct unary_function {
    typedef Arg argument_type;
    typedef Result result_type;
};

template <class Arg1, class Arg2, class Result>
struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
};      

template <class T>
struct negate
{
    T operator()(const T& x) const { return -x; }
};

template <class T>
struct equal_to
{
    bool operator()(const T& x, const T& y) const { return x == y; }
};

template <class T>
struct not_equal_to
{
    bool operator()(const T& x, const T& y) const { return x != y; }
};

template <class T>
struct greater
{
    bool operator()(const T& x, const T& y) const { return x > y; }
};

template <class T>
struct less: public binary_function<T, T, bool> 
{
    bool operator()(const T& x, const T& y) const { return x < y; }
};

template <class T>
struct greater_equal
{
    bool operator()(const T& x, const T& y) const { return x >= y; }
};

template <class T>
struct less_equal
{
    bool operator()(const T& x, const T& y) const { return x <= y; }
};


template <class Predicate>
class unary_negate : public unary_function<typename Predicate::argument_type, bool> {
protected:
    Predicate pred;
public:
    unary_negate(const Predicate& x) : pred(x) {}
    bool operator()(const argument_type& x) const { return !pred(x); }
};


template <class Predicate>
unary_negate<Predicate> not1(const Predicate& pred) {
    return unary_negate<Predicate>(pred);
}


template <class T, class U>
struct pair {
  T first;
  U second;

  pair(const T& t, const U& u): first(t), second(u) {}
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -