#include <deque>

在 C++中,<deque> 是标准模板库(STL)的一部分,它提供了双端队列(double-ended queue)的实现。

双端队列是一种允许在两端进行插入和删除操作的线性数据结构。

<deque> 的全称是 "double-ended queue",它在C++中以模板类的形式存在,允许存储任意类型的数据。

<deque> 是一个动态数组,它提供了快速的随机访问能力,同时允许在两端进行高效的插入和删除操作。这使得 <deque> 成为处理需要频繁插入和删除元素的场景的理想选择。

// <deque> -*- C++ -*-

// Copyright (C) 2001-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation 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.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation 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.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/** @file include/deque
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_DEQUE
#define _GLIBCXX_DEQUE 1

#pragma GCC system_header

#include <bits/stl_algobase.h>
#if __cplusplus > 201703L
#  include <bits/stl_algo.h> // For remove and remove_if
#endif // C++20
#include <bits/allocator.h>
#include <bits/stl_construct.h>
#include <bits/stl_uninitialized.h>
#include <bits/stl_deque.h>
#include <bits/range_access.h>
#include <bits/deque.tcc>

#ifdef _GLIBCXX_DEBUG
# include <debug/deque>
#endif

#if __cplusplus >= 201703L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  namespace pmr
  {
    template<typename _Tp> class polymorphic_allocator;
    template<typename _Tp>
      using deque = std::deque<_Tp, polymorphic_allocator<_Tp>>;
  } // namespace pmr
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++17

#if __cplusplus > 201703L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

#define __cpp_lib_erase_if 202002L

  template<typename _Tp, typename _Alloc, typename _Predicate>
    inline typename deque<_Tp, _Alloc>::size_type
    erase_if(deque<_Tp, _Alloc>& __cont, _Predicate __pred)
    {
      const auto __osz = __cont.size();
      __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred),
		   __cont.end());
      return __osz - __cont.size();
    }

  template<typename _Tp, typename _Alloc, typename _Up>
    inline typename deque<_Tp, _Alloc>::size_type
    erase(deque<_Tp, _Alloc>& __cont, const _Up& __value)
    {
      const auto __osz = __cont.size();
      __cont.erase(std::remove(__cont.begin(), __cont.end(), __value),
		   __cont.end());
      return __osz - __cont.size();
    }
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++20

#endif /* _GLIBCXX_DEQUE */
deque()默认构造函数,创建一个空的 deque 容器。
deque(size_type n)创建一个包含 n 个默认值元素的 deque 容器。
deque(size_type n, const T& value)创建一个包含 n 个值为 value 的 deque 容器。
deque(initializer_list<T> il)使用初始化列表 il 构造 deque 容器。
operator=赋值操作符,赋值给 deque 容器。
assign()用新值替换 deque 容器中的所有元素。
at(size_type pos)返回 pos 位置的元素,并进行范围检查。
operator[](size_type pos)返回 pos 位置的元素,不进行范围检查。
front()返回第一个元素的引用。
back()返回最后一个元素的引用。
begin()返回指向第一个元素的迭代器。
end()返回指向末尾元素后一位置的迭代器。
rbegin()返回指向最后一个元素的逆向迭代器。
rend()返回指向第一个元素之前位置的逆向迭代器。
empty()检查容器是否为空。
size()返回容器中的元素个数。
max_size()返回容器可容纳的最大元素个数。
clear()清除容器中的所有元素。
insert(iterator pos, const T& value)在 pos 位置插入 value 元素。
erase(iterator pos)移除 pos 位置的元素。
push_back(const T& value)在容器末尾添加 value 元素。
pop_back()移除容器末尾的元素。
push_front(const T& value)在容器前端添加 value 元素。
pop_front()移除容器前端的元素。
resize(size_type count)调整容器大小为 count,多出部分用默认值填充。
swap(deque& other)交换两个 deque 容器的内容。
get_allocator()返回一个用于构造双端队列的分配器对象的副本。
#include <iostream> #include <deque> #include <utility> #include <cstdlib> #include <ctime> #include <windows.h> // 替代跨平台输入方案 using namespace std; const int WIDTH = 20; const int HEIGHT = 20; // 使用Windows API实现kbhit int kbhit() { return _kbhit(); } // Windows下的getch实现 int getch() { return _getch(); } struct GameState { bool gameOver; int score; deque<pair<int, int> > snakeBody; // 解决>>语法问题 pair<int, int> food; // 食物坐标 char direction; // 当前移动方向 }; void initGame(GameState& game) { game.gameOver = false; game.score = 0; game.direction = 'R'; // 初始向右移动 // 初始化蛇身:起始长度为3,位置居中 game.snakeBody.clear(); game.snakeBody.push_back(make_pair(WIDTH/2, HEIGHT/2)); // 蛇头 game.snakeBody.push_back(make_pair(WIDTH/2-1, HEIGHT/2)); // 蛇身 game.snakeBody.push_back(make_pair(WIDTH/2-2, HEIGHT/2)); // 蛇尾 // 生成第一个食物 srand(static_cast<unsigned>(time(NULL))); // 避免time_t警告 game.food.first = rand() % WIDTH; game.food.second = rand() % HEIGHT; } void generateFood(GameState& game) { bool onSnake; do { game.food.first = rand() % WIDTH; game.food.second = rand() % HEIGHT; // 检查食物是否在蛇身上 onSnake = false; for (deque<pair<int, int> >::iterator it = game.snakeBody.begin(); it != game.snakeBody.end(); ++it) { if (it->first == game.food.first && it->second == game.food.second) { onSnake = true; break; } } } while (onSnake); // 直到生成不在蛇身上的食物 } void updateSnake(GameState& game) { // 根据方向计算新的蛇头位置 pair<int, int> newHead = game.snakeBody.front(); switch (game.direction) { case 'U': newHead.second--; break; // 上 case 'D': newHead.second++; break; // 下 case 'L': newHead.first--; break; // 左 case 'R': newHead.first++; break; // 右 } // 检查碰撞边界 if (newHead.first < 0 || newHead.first >= WIDTH || newHead.second < 0 || newHead.second >= HEIGHT) { game.gameOver = true; return; } // 检查碰撞自身 for (deque<pair<int, int> >::iterator it = game.snakeBody.begin(); it != game.snakeBody.end(); ++it) { if (it->first == newHead.first && it->second == newHead.second) { game.gameOver = true; return; } } // 在头部添加新位置 game.snakeBody.push_front(newHead); // 检查是否吃到食物 if (newHead.first == game.food.first && newHead.second == game.food.second) { game.score++; generateFood(game); // 生成新食物 } else { // 没吃到食物则移除尾部 game.snakeBody.pop_back(); } } void render(const GameState& game) { system("cls"); // Windows清屏 // 绘制上边界 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { // 绘制左边界 if (x == 0) cout << "#"; // 检查当前坐标内容 if (game.snakeBody.front().first == x && game.snakeBody.front().second == y) { cout << "O"; // 蛇头 } else { bool isBody = false; // 从第二个元素开始遍历蛇身 for (deque<pair<int, int> >::const_iterator it = game.snakeBody.begin() + 1; it != game.snakeBody.end(); ++it) { if (it->first == x && it->second == y) { cout << "o"; // 蛇身 isBody = true; break; } } if (!isBody) { if (game.food.first == x && game.food.second == y) { cout << "*"; // 食物 } else { cout << " "; } } } // 绘制右边界 if (x == WIDTH - 1) cout << "#"; } cout << endl; } // 绘制下边界 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 显示分数 cout << "Score: " << game.score << endl; } int main() { GameState game; initGame(game); while (!game.gameOver) { render(game); // 检测键盘输入 if (kbhit()) { char key = getch(); // 防止180度转向 switch (key) { case 'w': case 'W': if (game.direction != 'D') game.direction = 'U'; break; case 's': case 'S': if (game.direction != 'U') game.direction = 'D'; break; case 'a': case 'A': if (game.direction != 'R') game.direction = 'L'; break; case 'd': case 'D': if (game.direction != 'L') game.direction = 'R'; break; case 'x': case 'X': game.gameOver = true; break; } } updateSnake(game); Sleep(100); // 替代std::this_thread::sleep_for } render(game); cout << "Game Over! Final score: " << game.score << endl; system("pause"); // 避免窗口立即关闭 return 0; }上面完整版代码中在Dev-C++5.11版本中运行时第十五行代码(return _kbhit();)报错了,请修改报错,并确保能够在Dev-C++5.11版本中运行
06-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值