for(;;) vs do..while() for main program loop
for(;;) vs do..while() for main program loop
我正在使用do..while 循环允许我的程序在条件正确时退出,但我在学校时在示例中看到过为了退出而抛出异常的示例。这些示例使用了常规的无限 for 循环。我想我可以使用 if 语句来中断 for 循环而不是抛出,但我不确定。代码如下:
主.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "commands.h"
using namespace std;
int main(int argc, char *argv[]) {
bool exit = false;
try {
do {
try {
// Read a line, break at EOF, and echo print the prompt
// if one is needed.
string line;
getline(cin, line);
if (cin.eof()) {
cout << endl;
break;
}
command_fn fn = find_command_fn(line);
fn();
}
catch (command_error& error) {
// If there is a problem discovered in any function, an
// exn is thrown and printed here.
cout << error.what() << endl;
}
} while (!exit);
}
catch (exception& e) {
}
return 0;
}
命令.h
#ifndef __COMMANDS_H__
#define __COMMANDS_H__
#include <unordered_map>
using namespace std;
// A couple of convenient usings to avoid verbosity.
using command_fn = void (*)();
using command_hash = unordered_map<string,command_fn>;
// command_error -
// Extend runtime_error for throwing exceptions related to this
// program.
class command_error: public runtime_error {
public:
explicit command_error (const string& what);
};
// execution functions -
void fn_connect ();
void fn_disconnect ();
void fn_test ();
void fn_exit ();
command_fn find_command_fn (const string& command);
#endif
命令.cpp
#include "commands.h"
command_hash cmd_hash {
{"c" , fn_connect },
{"d" , fn_disconnect},
{"t" , fn_test },
{"e" , fn_exit },
};
command_fn find_command_fn (const string& cmd) {
// Note: value_type is pair<const key_type, mapped_type>
// So: iterator->first is key_type (string)
// So: iterator->second is mapped_type (command_fn)
const auto result = cmd_hash.find (cmd);
if (result == cmd_hash.end()) {
throw command_error (cmd + ": no such function");
}
return result->second;
}
command_error::command_error (const string& what):
runtime_error (what) {
}
void fn_connect (){
}
void fn_disconnect (){
}
void fn_test (){
}
void fn_exit (){
}
编辑:包含更多来源。我有很多空白,因为我正在重写一个通过UDP发送数据的程序。我只是在寻找一些关于如何安全退出该程序的建议。
do...while
完全等效于for
循环,只是测试是在循环结束时而不是开始时完成的,因此do...while
总是至少运行一次。在这两种情况下,您都可以使用 break
退出循环。您不需要抛出异常,但当然可以。如果你是多个级别的深度,通常更容易抛出一个异常,要么在循环中捕获它并中断,要么设置终止条件,要么在循环之外捕获异常,这也将终止它。
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 在Linux for Windows上编译C++代码时出错
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- std::memory_order for std::atomic:<T>:wait
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- for(;;) vs do..while() for main program loop