while(cin && cin.get() != '\n') continue;

本文详细解析了C++中cin.get()及其配套使用的场景。通过实例说明如何利用cin.get()来过滤超出长度限制的输入,确保输入数据的有效性和安全性。

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

在学习C++过程中,很多次遇到题目这个语句,终于懂了它是在干嘛(下面的解释如果哪儿不对,希望大家能给我讲一下)

cin.get(temp, MaxLen);

while(cin && cin.get() != '\n')

continue;


对于这一段话,首先,方法cin.get(temp, MaxLen)将一直读取输入,直到到达行尾或者是读取了 MaxLen-1 个字符。换行符被留在输入队列。while中的cin.get()读取上一句输入后的字符,直到读到换行符,它在做的事就是丢弃滤掉多余的字符。

所以这段话做的事是把超过MaxLen-1的部分滤掉。

比如,假设MaxLen=6,结果如下:



#include <iostream> #include <vector> #include <deque> #include <set> #include <random> // 更高质量的随机数生成 #include <ctime> #include <string> #include <unordered_set> using namespace std; // 二维坐标结构体 struct coord { int x; int y; }; // 全局变量 vector<string> Map; // 地图容器 short offset[4][2] = { // 四方向偏移量(右、下、左、上) {0, 1}, {1, 0}, {0, -1}, {-1, 0} }; int n; // 地图尺寸 coord Start, End; // 起点和终点坐标 // 地图打印函数 void print() { for (const auto& row : Map) { cout << row << endl; } } // 随机坐标生成(确保坐标在地图范围内且位置为空) bool random_crood(coord& c) { for(int attempt = 0; attempt < 100; attempt++) { c.x = rand() % n; c.y = rand() % n; if(Map[c.y][c.x] == ' ') return true; } return false; // 100次尝试失败后返回false } // 广度优先搜索(验证路径连通性) int bfs(coord start, coord end) { deque<coord> que; auto cmp = [](const coord& a, const coord& b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }; set<coord, decltype(cmp)> visited(cmp); que.push_back(start); visited.insert(start); while (!que.empty()) { coord now = que.front(); que.pop_front(); if(now.x == end.x && now.y == end.y) return true; for (int i = 0; i < 4; ++i) { coord next; next.x = now.x + offset[i][0]; next.y = now.y + offset[i][1]; if (next.x >= 0 && next.x < n && next.y >= 0 && next.y < n && Map[next.y][next.x] != '#' && !visited.count(next)) { que.push_back(next); visited.insert(next); } } } return false; // 返回是否连通 } // 递归分割法生成完美迷宫(无环且全连通) void generate_complex_maze(int top, int bottom, int left, int right) { // 基础条件:最小区域尺寸 if (bottom - top < 2 || right - left < 2) return; // 随机选择分割方向(纵向60%,横向40%) bool vertical = (rand() % 100) < 60; // 随机位置创建隔墙(避开边缘) int wall_pos = vertical ? left + 1 + rand() % (right - left - 1) : top + 1 + rand() % (bottom - top - 1); // 随机位置开洞(保证连通) int hole_pos = vertical ? top + rand() % (bottom - top) : left + rand() % (right - left); // 创建墙壁并保留一个通道 if (vertical) { for (int y = top; y < bottom; y++) { if (y != hole_pos) Map[y][wall_pos] = '#'; } // 递归处理左右子区域 generate_complex_maze(top, bottom, left, wall_pos-1); generate_complex_maze(top, bottom, wall_pos+1, right); } else { for (int x = left; x < right; x++) { if (x != hole_pos) Map[wall_pos][x] = '#'; } // 递归处理上下子区域 generate_complex_maze(top, wall_pos-1, left, right); generate_complex_maze(wall_pos+1, bottom, left, right); } } // 计算迷宫哈希值(避免重复) string get_maze_hash() { string hash_str; for(auto& row : Map) hash_str += row; return hash_str; } int main() { srand(time(0)); // 初始化随机种子 cin >> n; // 输入地图尺寸 // 地图初始化(n×n的空格) Map.resize(n, string(n, ' ')); // 生成不重复迷宫 unordered_set<string> maze_hashes; while(true) { // 1. 使用递归分割生成基础结构 generate_complex_maze(0, n, 0, n); // 2. 随机放置起点和终点 random_crood(Start); random_crood(End); Map[Start.y][Start.x] = 'S'; Map[End.y][End.x] = '@'; // 3. 添加随机障碍扰动(增加复杂度) for(int i = 0; i < n*n*0.15; i++) { coord c; if(random_crood(c) && Map[c.y][c.x] == ' ') { Map[c.y][c.x] = '#'; // 如果破坏连通性则撤销 if(!bfs(Start, End)) { Map[c.y][c.x] = ' '; } } } // 4. 验证路径唯一性 deque<coord> que; set<coord> visited; que.push_back(Start); visited.insert(Start); int path_count = 0; while (!que.empty()) { coord now = que.front(); que.pop_front(); if(now.x == End.x && now.y == End.y) { path_count++; if(path_count > 1) break; } for (int i = 0; i < 4; ++i) { coord next; next.x = now.x + offset[i][0]; next.y = now.y + offset[i][1]; if (next.x >= 0 && next.x < n && next.y >= 0 && next.y < n && Map[next.y][next.x] != '#' && !visited.count(next)) { que.push_back(next); visited.insert(next); } } } // 5. 检查迷宫唯一性 string hash = get_maze_hash(); if(path_count == 1 && maze_hashes.find(hash) == maze_hashes.end()) { maze_hashes.insert(hash); break; } // 重置地图继续生成 for(int y = 0; y < n; y++) { for(int x = 0; x < n; x++) { if(Map[y][x] == 'S' || Map[y][x] == '@') continue; Map[y][x] = ' '; } } } // 输出最终迷宫 print(); return 0; } 不需要考虑非标准库头文件、函数问题,请输出修改后的完整c++代码,并适量添加注释
最新发布
08-14
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值