【发布时间】:2020-08-09 03:58:24
【问题描述】:
我想比较存储在 filename[i] 和 filename[j] 中的值,并打印出 filename[i] 中与 filename[j] 中不同文件名的值。我知道可以使用 set_difference 和 sort 解决方案,但我不知道究竟要编写 sort 和 set_differences 代码。在这里,我提供了我的原始代码,以便您可以对其进行测试并更好地了解我要做什么。
我的完整代码:
#include <string>
#include <iostream>
#include <ctime> //important when to make random filename- srand(time(0))
#include <opencv2\opencv.hpp> //important when using opencv
#include <vector> //when using vector function
using namespace std;
using namespace cv; //important when using opencv
int main(int argc, char* argv[]) {
vector<String> filenames;
int a, i;
srand(time(0)); //seed random filenames - for random filename
// Get all jpg in the folder
cv::glob("C:\\Users\\x\\Documents\\Aggressive\\abc", filenames);
for (size_t i = 0; i < filenames.size(); i++)
{
Mat im = imread(filenames[i]); //read the filename location
std::cout << "\n";
std::size_t found = filenames[i].find_last_of("//\\");
//std:cout << " file: " << filenames[j].substr(found + 1) << '\n'; //display filename and its format (.jpg)
std::string::size_type const p(filenames[i].substr(found + 1).find_last_of('.')); //eg: 2.jpg then it will find the last '.'
std::string file_without_extension = filenames[i].substr(found + 1).substr(0, p); //eg: 2
std::cout << " file : " << filenames[i].substr(found + 1).substr(0, p); //display filename without .jpg
}
cout << "\n";
cout << "There's " << filenames.size() << " files in the current directory.\n" << endl; // total file in the specific directory
cout << "Enter array size: \n";
cin >> a;
for (int j = 0; j < filenames.size(); j++) {
//generate random filename
int index = rand() % filenames.size(); //random based on total of the file in the directory
//cout << filenames[index] << endl; //display the random number but might be redundant
//swap filenames[j] with filenames[index]
string temp = filenames[j];
filenames[j] = filenames[index];
filenames[index] = temp;
}
for (int j = 0; j < a; j++) {
//cout << "Random image selected:" << filenames[j] << endl; //basically to avoid the redundant random filename
Mat im = imread(filenames[j]); //read filename location
std::size_t found = filenames[j].find_last_of("//\\");
//std:cout << " file: " << filenames[j].substr(found + 1) << '\n'; //display filename and its format (.jpg)
std::string::size_type const p(filenames[j].substr(found + 1).find_last_of('.')); //eg: 2.jpg then it will find the last '.'
std::string file_without_extension = filenames[j].substr(found + 1).substr(0, p); //eg: 2
std::cout << " file: " << filenames[j].substr(found + 1).substr(0, p); //display filename without .jpg
string written_directory = "C:/Users/x/Documents/folder/" + filenames[j].substr(found + 1).substr(0, p) + ".jpg"; // write filename based on its original filename.
imwrite(written_directory, im);
}
return 0;
}
【问题讨论】:
-
你有什么?你想得到什么?好像您有 2 个
std::string数组。您想打印一个数组中的所有元素,而不是另一个。我不太明白您是否以及为什么要对其进行排序?你能告诉我我做对了什么吗? -
实际上,对于数组 1,它会显示一个文件夹中的所有文件名,而在数组 2 中,我有一个基于用户输入的数组大小的随机文件名。所以我现在的目标是我想打印出数组 2 中没有的所有文件名。这就是为什么我想对两个数组进行排序并设置这些数组之间的差异,以便我可以打印出不同的值文件名。
-
标准库为您服务:std::set_difference.
-
@JesperJuhl 我已经尝试根据我的理解来编写代码,但似乎我犯了很多错误。所以可悲的是它对我不起作用。顺便说一句,我是编程的初学者,所以我不太了解它是如何工作的。
-
@WBuck 'array' 是什么意思足够小?以及如何使用二次方法?
标签: c++ arrays sorting set-difference