由于经常碰巧从 .csv 或 .txt 文件中读取此类字符串,我想知道获取 %d/%m%/%y
(或任何其他类似格式)字符串并将其转换为适合 QuantLib::Date
对象的内容 constructor .
下面是示例代码:
#include <ql/quantlib.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <iomanip>
#include <boost/algorithm/string.hpp>
int main() {
boost::timer timer;
std::cout << std::endl;
std::string datesString = {
",17/10/2014,21/11/2014,19/12/2014,20/03/2015,19/06/2015,18/09/2015,18/12/2015,17/06/2016,"
};
std::vector<std::string> expiryDates;
boost::split(expiryDates, datesString, boost::is_any_of(","));
for(int i = 0; i < expiryDates.size(); i++)
{
std::cout << expiryDates[i] << std::endl;
}
// 17/10/2014
// 21/11/2014
// 19/12/2014
// 20/03/2015
// 19/06/2015
// 18/09/2015
// 18/12/2015
// 17/06/2016
// QuantLib::Date myQLDate(?);
return 0;
}
最佳答案
有点隐藏,但是一旦包含 <ql/utilities/dataparsers.hpp>
你可以使用:
Date d = DateParser::parseFormatted(expiryDates[i], format);
哪里format
是一个 Boost.Date 格式字符串。在你的情况下,
Date d = DateParser::parseFormatted(expiryDates[i], "%d/%m/%Y");
应该可以解决问题。
关于c++ - 将日期 std::string 转换为 QuantLib::Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301337/