使用boost来访问文件夹中的每一个文件, 通过迭代器进行迭代时,每次都是随机的,不按顺序(当需要按顺序读取图片时,就会有问题) 因此需要先对文件进行排序: namespace bf = boost::filesystem; bf::path path_l("/home/wk/DataSet/GRC/route01/seq01/image_0"); //default seq04 bf::directory_iterator dir_begin_l(path_l); //-- boost的迭代起每次是随机访问下一个,不是按顺序的 bf::directory_iterator dir_end_l; std::vector<long> files_l; char name_str_l[50]; for (; dir_begin_l != dir_end_l; ++dir_begin_l) { sscanf(dir_begin_l->path().filename().c_str(), "%*4s%[^.]", name_str_l); files_l.push_back(atol(name_str_l)); //std::cout << "left:" << *dir_begin_l <<std::endl; } sort(files_l.begin(), files_l.end(), less<long>());