#include <utility>

#include <utility>这个头文件是什么用法

utility头文件定义了一个pair类型,是标准库的一部分,其原型为:
template<class _Ty1,
class _Ty2> struct pair
{ // store a pair of values
typedef pair<_Ty1, _Ty2> _Myt;
typedef _Ty1 first_type;
typedef _Ty2 second_type;

pair()
: first(_Ty1()), second(_Ty2())
{ // construct from defaults
}

pair(const _Ty1& _Val1, const _Ty2& _Val2)
: first(_Val1), second(_Val2)
{ // construct from specified values
}

template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}

void swap(_Myt& _Right)
{ // exchange contents with _Right
if (this != &_Right)
{ // different, worth swapping
std::swap(first, _Right.first);
std::swap(second, _Right.second);
}
}

_Ty1 first; // the first stored value
_Ty2 second; // the second stored value
};
可以看出这是一个模板类,定义了两个数据成员:first、second和一组成员函数。
有两个普通构造函数和一个复制构造函数,具体的还定义了一个make_pair函数和一些操作符,
函数的实现你可以看看标准库的utility头文件。另外,map的元素类型就是pair类型~


 
 

例子

#include <iostream>

#include <utility>

using namespace std;

pair<int, int> p;

int main()

{

cin >> p.first >> p.second;

cout << p.first << " " << p.second << endl;

return 0;

}


#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值