从一个文件和另一个单独的文件中取出时间,并将两个文件中的时间值放入c++列表中
Take time from one file and another separate file and place the time values from both files into a list c++
我正在尝试加载两个文件,但是我很难将文件1和文件2中输入的时间,并将它们放入包含所有时间的列表中,但如果时间已经输入,则不会再次输入。
这不是家庭作业。我只是在自己做一个应用程序项目。
——我建立了一个时间列表,在一个集合中的每个文件,然后加载的时间已经被放置到一个地图只有一个列表的时间,但我不知道从哪里开始。明白了吗?
简单地读取文件,在(un)ordered_map
中插入读取的次数,如果要插入的条目已经添加,则跳到下一个迭代。
#include <unordered_map>
std::unordered_map<std::string, VALUE_TYPE> hash;
while (read line) {
std::string date(extract_date(line));
auto it(hash.insert(std::make_pair(date, VALUE_TYPE())));
// If you want to check whether the last value has been inserted
if (it.second) {
// do something with the pair it.first
}
}
:
在map
中,无论是否有序,您都在映射值,因此,对于每个A
类型的值,您将保存一些B
类型的值。
在这种情况下,一旦您必须为每个日期保存一些VALUE_TYPE
值,您可以简单地获取时间字符串,将其视为一个键,并且,如果键已经添加到映射容器中,则不会再次插入它—将保留在容器中的VALUE_TYPE
值将是初始值。
std::map<int, int> map;
map.insert(std::make_pair(1, 1));
map.insert(std::make_pair(2, 1));
map.insert(std::make_pair(1, 2));
map.insert(std::make_pair(2, 2));
for (auto it(map.begin()); it != map.end(); ++it) {
std::cout << it->first << " " << it->second << std::endl;
}
输出:1 1
2 1
如果时间值在文件中排序,则最好使用std::list<time>
来完成任务。将file1和file2中的值按顺序读入不同的列表中,并按此顺序使用list::merge
和list::unique
。这是因为std::set
和std::map
都不保留它们存储的值的顺序。
您需要使用list::push_back, list::merge和list::unique,并且每个文档页面都包含一个示例。
相关文章:
- 从文本文件中读取时钟时间和事件时间并进行处理
- Levenshtein 两个文件的距离花费了太多时间
- 如何从远程 SFTP 服务器获取 HH-MM-SS 时间戳格式的文件列表
- 如何将消息时间戳写入日志文件?
- Chilkat SFTP 文件修改时间不与上传更新
- 如何在 Windows 上的 C++ 中根据文件的修改时间读取目录中的文件
- 正在等待在非阻塞文件描述符上长时间运行ioctl
- C++-需要读取存储在文件(.txt)中的时间戳并正确检索
- 测量任何 Windows 可执行文件的内存使用情况和执行时间
- std::fstream 需要很长时间才能将大数据写入.csv文件中
- 如何减少编译时间:在包含未触及的头文件的情况下
- 当字节在文件上写入0时,是否可以保持写作时间稳定
- C++ 如何使用字符串中保存的文件路径创建带时间戳的目录
- 使用LTO静态链接的可执行文件(链接时间优化):如何使用以前构建的库进行制作
- 在C++中将数据和时间分配给文件时间结构
- VC++ 中的文件时间结构
- c++ 将文件时间转换为秒
- 系统时间、本地时间、文件时间和正常时间
- 为什么我的程序终止调用KERNEL32!文件时间到系统时间 ()
- CUDA中的系统时间和文件时间