提升标准::矢量<double>的自定义验证器
Boost custom validator for std::vector<double>
我为std::vector<double>
编写了以下自定义验证器。
typedef vector<double> coordinate;
void validate(boost::any& v,
const vector<string>& values,
coordinate*, int) {
std::cout << "Custom validator calledn";
coordinate c;
vector<double> dvalues;
for(vector<string>::const_iterator it = values.begin();
it != values.end();
++it) {
stringstream ss(*it);
copy(istream_iterator<double>(ss), istream_iterator<double>(),
back_inserter(dvalues));
if(!ss.eof()) {
std::cerr << "SS EOFn";
throw po::invalid_option_value("Invalid coordinate specification sseof");
}
}
if(dvalues.size() != 2) {
std::cerr << "dvalues sizen";
throw po::invalid_option_value("Invalid coordinate specification dvalues size");
}
c.push_back(dvalues[0]);
c.push_back(dvalues[1]);
v = c;
}
我以以下方式添加选项:
coordinate c;
// Setup options.
po::options_description desc("Options");
desc.add_options()
("instruments.prop", po::value<coordinate>( &c )->multitoken(),
"plugin names" );
程序根本不使用自定义验证器。我没有收到消息"自定义验证器"的消息,如果使用了我的验证器,它应该打印。相反,我得到了这个错误:
丢弃一个实例后终止 'boost :: exception_detail :: clone_impl
'what((:选项'instruments.name'的参数('1 2.9'(被无效中止(核心倾倒(
我的配置文件看起来像:
[仪器]
prop = 1 2.9
关于如何从配置文件中解析多个参数的任何想法,而无需在这样的单独行中编写它们:
[仪器]
prop = 1
prop = 2.9
您要编写自定义转换:
活在coliru
#include <boost/program_options.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <fstream>
#include <iostream>
namespace po = boost::program_options;
typedef std::vector<double> coordinate;
int main() {
coordinate c;
// Setup options.
po::options_description desc("Options");
desc.add_options()
("instruments.prop", po::value<std::string>()->multitoken()->notifier([&c](std::string const& v) {
auto it = boost::make_split_iterator(v, boost::token_finder(boost::algorithm::is_any_of(" ,")));
std::transform(it, {}, back_inserter(c), [](auto& s) {
return boost::lexical_cast<double>(s);
});
}),
"plugin names" );
std::ifstream ifs("input.txt");
po::variables_map vm;
store(po::parse_config_file(ifs, desc, false), vm);
po::notify(vm);
std::copy(c.begin(), c.end(), std::ostream_iterator<double>(std::cout << "c: ", " "));
std::cout << "n";
}
打印
c: 1 2.9
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 创建具有验证和语法突出显示的自定义 printf
- boost::p rogram_options 自定义验证和默认值
- boost program_options自定义验证
- 提升标准::矢量<double>的自定义验证器
- 提升程序选项:自定义验证器是否需要重载运算符>>?
- 自定义验证器不允许default_value
- 用于提升program_options的自定义验证器不适用于 GCC,适用于 MSVC
- 为什么 Boost 使用全局函数覆盖来实现自定义验证程序"Program Options"
- 自定义Windows身份验证包