这道题好难,根本不知道什么意思,请各位大佬帮忙解一下
参考stack,设计一个类,能够实现对环形队列的读写。其中包括对队列类型、队列总长度的定义、初始化、为空判断、为满判断、读取、写入、获取队列有效内容长度等。
2020-03-22 10:18
程序代码:#include <cstddef>
#include <stdexcept>
template<typename T, size_t N>
class foo
{
public:
foo() = default;
foo( const foo& ) = default;
foo& operator=( const foo& ) = default;
bool empty() const noexcept
{
return _size == 0;
}
bool full() const noexcept
{
return _size == N;
}
size_t size() const noexcept
{
return _size;
}
const T& operator[]( size_t index ) const
{
return _data[ (_head+index)%N ];
}
T& operator[]( size_t index )
{
return _data[ (_head+index)%N ];
}
const T& at( size_t index ) const
{
if( index >= N )
throw std::out_of_range();
return _data[ (_head+index)%N ];
}
T& at( size_t index )
{
if( index >= N )
throw std::out_of_range();
return _data[ (_head+index)%N ];
}
void push_back( const T& value )
{
_data[(_head+_size++)%N] = value;
}
void pop_back( const T& value )
{
--_size;
}
void push_front( const T& value )
{
_head = (_head+N-1)%N;
++_size;
_data[_head] = value;
}
void pop_front( const T& value )
{
_head = (_head+1)%N;
--_size;
}
private:
T _data[N];
size_t _head;
size_t _size;
};
2020-03-22 12:37
2020-03-22 13:36
2020-03-23 08:34
程序代码: void pop_front( const T& value )
{
_head = (_head+1)%N;
--_size;
}
2020-03-23 11:22
程序代码:#include <cstddef>
#include <stdexcept>
#include <cassert>
template<typename T, size_t N>
class foo
{
public:
foo() : _head(), _size()
{
}
foo( const foo& ) = default;
foo& operator=( const foo& ) = default;
bool empty() const noexcept
{
return _size == 0;
}
bool full() const noexcept
{
return _size == N;
}
size_t size() const noexcept
{
return _size;
}
const T& operator[]( size_t index ) const noexcept
{
assert( index < N );
return ((const T*)_data)[ (_head+index)%N ];
}
T& operator[]( size_t index ) noexcept
{
assert( index < N );
return ((T*)_data)[ (_head+index)%N ];
}
const T& at( size_t index ) const noexcept
{
if( index >= N )
throw std::out_of_range();
return operator[](index);
}
T& at( size_t index ) noexcept
{
if( index >= N )
throw std::out_of_range();
return operator[](index);
}
const T& front() const noexcept
{
assert( !empty() );
return operator[](0);
}
T& front() noexcept
{
assert( !empty() );
return operator[](0);
}
const T& back() const noexcept
{
assert( !empty() );
return operator[](_size-1);
}
T& back() noexcept
{
assert( !empty() );
return operator[](_size-1);
}
void clear()
{
for( size_t index=0; index!=_size; ++index )
operator[](_size-1-index).~T();
_head = 0;
_size = 0;
}
void push_front( const T& value )
{
assert( !full() );
_head = (_head+N-1)%N;
++_size;
new (&front())T( value );
}
void push_back( const T& value )
{
assert( !full() );
++_size;
new (&back())T( value );
}
void pop_front()
{
assert( !empty() );
front().~T();
_head = (_head+1)%N;
--_size;
}
void pop_back()
{
assert( !empty() );
back().~T();
--_size;
}
private:
char _data[N*sizeof(T)];
size_t _head;
size_t _size;
};
#include <iostream>
using namespace std;
int main( void )
{
auto prtall = []( const auto& f ) {
cout << '[';
for( size_t i=0; i!=f.size(); ++i )
cout << ' ' << f[i];
cout << " ]";
if( f.empty() )
cout << " empty";
if( f.full() )
cout << " full";
if( !f.empty() )
cout << " front=" << f.front() << " back=" << f.back();
cout << endl;
};
foo<int,3> a;
prtall( a );
a.push_back( 2 );
prtall( a );
a.push_front( 1 );
prtall( a );
a.push_back( 3 );
prtall( a );
a.pop_back();
prtall( a );
a.pop_front();
prtall( a );
a.clear();
prtall( a );
}
2020-03-23 14:12
,我还没学这种,只学了点C,现在在学C++
2020-03-23 19:38