查找字符串中的 **date** 并将其转换为其数字形式
Find and convert a **date** in a string to its numeric form
我正在从文档中提取日期字符串。日期可以具有以下格式:
- 12 1月 2014
- 六月12 1999
- 1999年 2月9日
- 2008-2-7
我想检查字符串是否包含月份的名称,然后将其转换为数字形式。例如:12 JAN 2014 ==> 12 1 2014, June 12 1999 ==> 6 12 1999 等。
我提出了以下解决方案:首先,我创建一个包含日期及其数值的文件
JANVIER 1
FEVRIER 2
MARS 3
AVRIL 4
MAI 5
JUIN 6
JUILLET 7
AOUT 8
SEPTEMBRE 9
OCTOBRE 10
NOVEMBRE 11
DECEMBRE 12
#include <boost/algorithm/string/replace.hpp>
#include <vector>
#include <iostream>
using namespace std;
vector<pair<string,int>> getDates();
bool dateExists(string,strin);
int main()
{
//replace JANVIER
string date = "JANVIER-12-1999";
vector<pair<string,int>> d = getDates("dates.txt");
for(size_t i = 0; i < d.size();i++)
{
string searchDate = d[i].first;
if(dateExists(date,searchDate ))
{
string num = std::to_string(d[i].second);
boost::replace_all(date, searchDate ,num );
}
}
cout << date << endl;
return 0;
}
vector<pair<string,int>> getDates(string path)
{
vector<pair<string,int>> vec;
boost::iostreams::stream<boost::iostreams::file_source> file(path.c_str());
string line;
while (std::getline(file, line))
{
std::vector<string> splitLine;
boost::split(splitLine,line,boost::is_any_of("t"));
vec.push_back(make_pair(splitLine[0],atoi(splitLine[1].c_str())));
}
return vec;
}
bool dateExists(string str,string str2)
{
if (str.find(str2) != string::npos)
return true;
else
return false;
}//end function
相关文章:
- 将数字转换为字母(例如:123 转换为一二三)
- 将数字转换为单词
- 使用if-else将数字转换为单词
- 将数字转换为填充字符串
- 函数在许多数字上转换为基数 1 时减去 2?
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 将 C 函数转换为 C++ 以检查数字是否有效
- 编译时间文本到数字转换 (atoi)
- 以C++格式将字符串转换为数字
- 在 c++ 中将数字转换为矢量数组的函数
- 奇怪的数字转换C++
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 用C++将浮点数字转换为本地化字符串
- 我需要静态转换我的数字模板值吗
- 如何将boost::asio::ip::address_v6 ip转换为2个uint64_t数字,并从2个uint 64
- Rcpp - 用二进制函数转换数字向量?
- 我如何使用堆栈转换数字从基数10到基数2在c++
- 如何在c++中从字节数组中的整数转换数字
- c++程序转换数字的位数