C++ young 程序库——y_queue.hpp 和 y_stack.hpp

文件位置:young/y_queue.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_QUEUE_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_QUEUE_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_functional.hpp"
#include "y_deque.hpp"
#include "y_vector.hpp"
#include "algorithm/y_algorithm_heap.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T, typename Container = deque<T> >
class queue
{
public:
    typedef  queue<T, Container>  self;
    typedef  Container  container_type;

    typedef  typename Container::value_type       value_type;
    typedef  typename Container::reference        reference;
    typedef  typename Container::const_reference  const_reference;
    typedef  typename Container::pointer          pointer;
    typedef  typename Container::const_pointer    const_pointer;
    typedef  typename Container::size_type        size_type;
    typedef  typename Container::difference_type  difference_type;

private:
    Container con;

public:
    explicit queue( const Container& other = Container() ) : con(other)  {}

    bool empty() const              {  return con.empty();  }
    size_type size() const          {  return con.size();  }
    reference front()               {  return con.front();  }
    const_reference front() const   {  return con.front();  }
    reference back()                {  return con.back();  }
    const_reference back() const    {  return con.back();  }
    void push( const_reference x )  {  con.push_back(x);  }
    void pop()                      {  con.pop_front();  }
    void swap( self& rhs )          {  con.swap( rhs.con );  }

    bool operator==( const self& rhs ) const  {  return ( con == rhs.con );  }
    bool operator!=( const self& rhs ) const  {  return ( con != rhs.con );  }
    bool operator<( const self& rhs ) const   {  return ( con < rhs.con );  }
    bool operator>( const self& rhs ) const   {  return ( con > rhs.con );  }
    bool operator<=( const self& rhs ) const  {  return ( con <= rhs.con );  }
    bool operator>=( const self& rhs ) const  {  return ( con >= rhs.con );  }
};

template< typename T, typename Container >
inline void swap( queue<T, Container>& lhs, queue<T, Container>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T, typename Container = vector<T>,
          typename Compare = less<typename Container::value_type> >
class priority_queue
{
public:
    typedef  priority_queue<T, Container, Compare>  self;
    typedef  Container  container_type;

    typedef  typename Container::value_type       value_type;
    typedef  typename Container::reference        reference;
    typedef  typename Container::const_reference  const_reference;
    typedef  typename Container::pointer          pointer;
    typedef  typename Container::const_pointer    const_pointer;
    typedef  typename Container::size_type        size_type;
    typedef  typename Container::difference_type  difference_type;

private:
    Container con;
    Compare  comp;

public:
    explicit priority_queue( const Compare& cmp = Compare() )
             : con(), comp(cmp)  {}

    template< typename InputIterator >
    priority_queue( InputIterator first, InputIterator last )
    : con(first, last), comp()
    {
        make_heap( con.begin(), con.end(), comp );
    }

    template< typename InputIterator >
    priority_queue( InputIterator first, InputIterator last, const Compare& cmp )
    : con(first, last), comp(cmp)
    {
        make_heap( con.begin(), con.end(), comp );
    }

    bool empty() const           {  return con.empty();  }
    size_type size() const       {  return con.size();  }
    const_reference top() const  {  return con.front();  }
    void swap( self& rhs )       {  con.swap( rhs.con );  }

    void push( const_reference value )
    {
        con.push_back( value );
        push_heap( con.begin(), con.end(), comp );
    }

    void pop()
    {
        pop_heap( con.begin(), con.end(), comp );
        con.pop_back();
    }
};

template< typename T, typename Container, typename Compare >
inline void swap( priority_queue<T, Container, Compare>& lhs,
                  priority_queue<T, Container, Compare>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

文件位置:young/y_stack.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_STACK_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_STACK_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_deque.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T, typename Container = deque<T> >
class stack
{
public:
    typedef  stack<T, Container>  self;
    typedef  Container            container_type;

    typedef  typename Container::value_type       value_type;
    typedef  typename Container::reference        reference;
    typedef  typename Container::const_reference  const_reference;
    typedef  typename Container::pointer          pointer;
    typedef  typename Container::const_pointer    const_pointer;
    typedef  typename Container::size_type        size_type;
    typedef  typename Container::difference_type  difference_type;

protected:
    Container con;

public:
    explicit stack( const Container& other = Container() ) : con(other)  {}

    bool empty() const              {  return con.empty();  }
    size_type size() const          {  return con.size();  }
    reference top()                 {  return con.back();  }
    const_reference top() const     {  return con.back();  }
    void push( const_reference x )  {  con.push_back( x );  }
    void pop()                      {  con.pop_back();  }
    void swap( self& rhs )          {  con.swap( rhs.con );  }

    bool operator==( const self& rhs ) const  {  return ( con == rhs.con );  }
    bool operator!=( const self& rhs ) const  {  return ( con != rhs.con );  }
    bool operator<( const self& rhs ) const   {  return ( con < rhs.con );  }
    bool operator>( const self& rhs ) const   {  return ( con > rhs.con );  }
    bool operator<=( const self& rhs ) const  {  return ( con <= rhs.con );  }
    bool operator>=( const self& rhs ) const  {  return ( con >= rhs.con );  }
};

template< typename T, typename Container >
inline void swap( stack<T, Container>& lhs, stack<T, Container>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

`std::queue` 是 C++ 标准库中的一个容器适配器,它提供了先进先出(FIFO,First In First Out)的数据结构。`emplace` 和 `push` 都是用来添加元素到队列尾部的操作,但它们有一些不同。 1. **std::queue::push**: - 这是一个模板成员函数,用于直接将一个可放的对象推入队列。如果对象不能被放置(例如,`push` 的类型不匹配队列的内部类型),就会抛出异常 `std::bad_alloc` 或者 `std::length_error`,具体取决于容器的行为。 - 语法通常是这样的: ```cpp template <class T> void push(const T& value); ``` - 示例: ```cpp std::queue<int> q; q.push(5); // 将整数 5 放入队列 ``` 2. **std::queue::emplace**: - 这是一个模板成员函数,采用前向移动或构造的方式插入元素。与 `push` 不同的是,`emplace` 允许你在构造新对象时提供默认参数或者使用 lambda 表达式进行初始化,这通常在你希望对象在队列内部被正确构造的情况下很有用。 - 语法类似于 `push`,但可以在构造新元素的同时传递额外的参数: ```cpp template <class... Args> void emplace(Args&&... args); ``` - 示例: ```cpp std::queue<std::string, std::deque<char>> q; q.emplace("Hello"); // 使用 deque 内部构造一个字符串 ``` - 如果没有提供足够的参数完成构造,`emplace` 也会抛出异常。 总结一下: - `push` 更简单,直接添加已存在的对象; - `emplace` 提供了构造新对象的功能,可能更高效,因为不需要拷贝或移动数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值