vector(pair (int, Vector2d))

本文演示了如何使用C++标准库中的vector容器来存储pair类型的元素,其中pair的第一个元素为int类型,第二个元素为Eigen库中的Vector2d类型。通过实例展示了向vector中添加元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vector<pair<int, Vector2d>>赋值操作
#include<iostream>
#include<vector>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
	vector<pair<int, Vector2d>> test;
	test.push_back(make_pair(3, Vector2d{ 2.43,4.56}));
	cout << "test:   ";
	cout << test[0].first << endl;
	cout << test[0].second;
	system("pause");
	return 0;
}

void GameManager::Run1_1() { gotoxy(0, 0); DrawII(); ProcessInput(); NPCstep(); Sleep(10); } int GameManager::autoFindWay(int map[20][40], int startX, int startY, int targetX, int targetY) { vector<vector<bool>> visited(20, vector<bool>(40, false)); vector<vector<pair<int, int>>> parent(20, vector<pair<int, int>>(40)); // 正确设置起点和终点的行列 int startRow = startY; // 行是startY int startCol = startX; // 列是startX int targetRow = targetY; // 目标行是targetY int targetCol = targetX; // 目标列是targetX queue<pair<int, int>> q; q.push({ startRow, startCol }); visited[startRow][startCol] = true; int dy[] = { -1, 1, 0, 0 }; // 行变化:上、下 int dx[] = { 0, 0, -1, 1 }; // 列变化:左、右 bool found = false; while (!q.empty()) { auto current = q.front(); q.pop(); // 判断是否到达目标点(正确的行和列) if (current.first == targetRow && current.second == targetCol) { found = true; break; } for (int i = 0; i < 4; i++) { int ny = current.first + dy[i]; // 新行 int nx = current.second + dx[i]; // 新列 // 检查边界和可通行性 if (ny >= 0 && ny < 20 && nx >= 0 && nx < 40 && map[ny][nx] == 0 && !visited[ny][nx]) { visited[ny][nx] = true; parent[ny][nx] = current; q.push({ ny, nx }); } } } if (!found) { cout << "No path found from (" << startRow << ", " << startCol << ") to (" << targetRow << ", " << targetCol << ")" << endl; return 0; } // 回溯路径 pair<int, int> node = { targetRow, targetCol }; pair<int, int> start = { startRow, startCol }; vector<pair<int, int>> path; while (node != start) { path.push_back(node); node = parent[node.first][node.second]; } path.push_back(start); reverse(path.begin(), path.end()); if (path.size() < 2) return 0; // 确定第一步方向 auto firstStep = path[1]; int dirY = firstStep.first - startRow; // 行变化 int dirX = firstStep.second - startCol; // 列变化 if (dirY == -1) return 1; // 上 else if (dirY == 1) return 2; // 下 else if (dirX == -1) return 3; // 左 else if (dirX == 1) return 4; // 右 return 0; }
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值