main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #ifndef PHOTO_FILE_PROCESSOR_H #define PHOTO_FILE_PROCESSOR_H #include <iostream> #include <string> #include <vector> #include <dirent.h> #include <algorithm> #include <stdexcept> class PhotoFileProcessor { public : // 构造函数,初始化目录路径 explicit PhotoFileProcessor( const std::string& directory_path) : directoryPath(directory_path) {} // 获取文件名(不带扩展名)并按时间戳排序的实现 std::vector<std::string> getFilenamesSortedByTimestamp() const { DIR* dir = opendir(directoryPath.c_str()); if (dir == nullptr ) { throw std::runtime_error( "无法打开目录: " + directoryPath); } std::vector<std::pair< double , std::string>> fileTimestampPairs; struct dirent* entry; while ((entry = readdir(dir)) != nullptr ) { std::string filename(entry->d_name); // 过滤掉"."和".."目录 if (filename == "." || filename == ".." ) { continue ; } // 去除文件扩展名 std::string filename_without_extension = removeExtension(filename); // 转化成时间戳 try { double timestamp = extractTimestamp(filename_without_extension); fileTimestampPairs.emplace_back(timestamp, filename_without_extension); } catch ( const std::runtime_error& e) { // 处理提取时间戳失败的情况 std::cerr << "警告: " << e.what() << std::endl; } } closedir(dir); // 按时间戳排序 std::sort(fileTimestampPairs.begin(), fileTimestampPairs.end(), []( const std::pair< double , std::string>& a, const std::pair< double , std::string>& b) { return a.first < b.first; } ); // 提取排序后的文件名 std::vector<std::string> sortedFilenames; for ( const auto & pair : fileTimestampPairs) { sortedFilenames.push_back(pair.second); } return sortedFilenames; } private : // 去除文件名中的扩展名的实现 std::string removeExtension( const std::string& filename) const { size_t last_dot = filename.find_last_of( '.' ); if (last_dot == std::string::npos) { return filename; // 如果没有点,返回原始文件名 } return filename.substr(0, last_dot); } // 从文件名中提取时间戳的实现 double extractTimestamp( const std::string& filename) const { // 假设文件名是数字形式的时间戳,例如 "1234567890.jpg" try { return std::stod(filename); } catch ( const std::invalid_argument& e) { throw std::runtime_error( "无法从文件名中提取时间戳: " + filename); } } // 目录路径 std::string directoryPath; }; int main() { try { std::string directory_path = "/home/dongdong/2project/0data/NWPU/img" ; // 替换为你的目录路径 PhotoFileProcessor processor(directory_path); std::vector<std::string> filenames = processor.getFilenamesSortedByTimestamp(); // 打印结果 for ( const std::string& filename : filenames) { std::cout << filename << std::endl; } } catch ( const std::exception& e) { std::cerr << "错误: " << e.what() << std::endl; } return 0; } #endif // PHOTO_FILE_PROCESSOR_H |
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 | cmake_minimum_required(VERSION 3.10) project(ImageUtilExample VERSION 1.0) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) # 添加可执行文件 add_executable(ImageUtilExample main.cpp ) # 如果需要,链接其他库 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
2022-08-18 (经典)ESP8266接收arduino数据上报onenet网站(温湿度 OLED屏幕 气压计)
2021-08-18 python使用百度api识别口罩-
2020-08-18 yolo训练自己的数据