由于经常从.csv或.txt文件中读取这些字符串,我想知道获取%d/%m%/%y(或任何其他类似格式)字符串并将其转换为适合QuantLib::Date对象构造函数的最简单方法.
下面是一个示例代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
它有点隐藏,但一旦你包括<ql/utilities/dataparsers.hpp>你可以使用:
Date d = DateParser::parseFormatted(expiryDates[i], format);
Run Code Online (Sandbox Code Playgroud)
其中format是Boost.Date格式字符串.在你的情况下,
Date d = DateParser::parseFormatted(expiryDates[i], "%d/%m/%Y");
Run Code Online (Sandbox Code Playgroud)
应该做的伎俩.
| 归档时间: |
|
| 查看次数: |
1813 次 |
| 最近记录: |