我有以下代碼:
/*
This function conditions folders into a standard format with the following specifications
The first file in the folder to render has an index starting at 0
with the name of outPutName[Index].fileExtension dataType is preserved
*/
void vCanvas::conditionFolder(vizModule target){
vector<string> fileNames;
int index = 0;
string outputName = "output";
string oPath = target.path;
string temp;
cout << "vCanvas::conditionFolder is processing" << oPath << endl;
//https://en.cppreference.com/w/cpp/filesystem/rename
for (const auto & entry : fs::directory_iterator(oPath)){
if (entry.is_regular_file()){
temp = entry.path().stem().string();
cout << oPath temp << " modified to " << oPath (outputName to_string(index) "." target.dataType) << endl;
fs::rename(oPath temp, oPath (outputName to_string(index) "." target.dataType));
index ;
}
}
}
我希望fs::directoryiterator能entry按字母順序給我s,但事實并非如此。我在 0001.png 之前得到 0007.png,這是行不通的。是否有任何簡單的解決方案可以讓我按字母順序獲取檔案夾中的所有檔案并重命名它們?
對上述代碼進行第二次迭代,使用 std::sort
//see https://www.cplusplus.com/reference/algorithm/sort/
bool alphabetSort(string i, string j){
int val = i.compare(j);
if (val == -1) {return true;}
else if (val == 1) {return false;}
else {return false;}
}
/*
This function conditions folders into a standard format with the following specifications
The first file in the folder to render has an index starting at 0
name of outPutName. dataType is preserved
*/
void vCanvas::conditionFolder(vizModule target){
string outputName = "output";
string oPath = target.path;
string temp;
vector<string> fileStrings;
cout << "vCanvas::conditionFolder is processing" << oPath << endl;
//https://en.cppreference.com/w/cpp/filesystem/rename
for (const auto & entry : fs::directory_iterator(oPath)){
if (entry.is_regular_file()){
temp = entry.path().stem().string();
fileStrings.push_back(temp);
}
}
for (int i = 0; i < fileStrings.size(); i ){
cout << fileStrings[i] << ",";
}
sort(fileStrings.begin(), fileStrings.end(),alphabetSort);
cout << "Directory sorted" << endl;
for (int i = 0; i < fileStrings.size(); i ){
cout << fileStrings[i] << ",";
}
cout << endl;
for (int k = 0; k < fileStrings.size(); k ){
temp = fileStrings[k];
cout << oPath temp << " modified to " << oPath (outputName to_string(k) "." target.dataType) << endl;
fs::rename(oPath temp, oPath (outputName to_string(k) "." target.dataType));
}
}
這使
vCanvas::conditionFolder is processing./unit_tests_folder/testpng1/
0007,0014,0018,0008,0012,0002,0005,0010,0016,0003,0001,0006,0015,0019,0017,0004,0011,0013,0009,0020,
0007,0009,0008,0002,0005,0004,0003,0001,0006,0014,0013,0011,0017,0019,0015,0016,0010,0012,0018,0020,
uj5u.com熱心網友回復:
我撰寫并運行了這個函式的單元測驗,它完成了我想要的,具體來說,首先按字母順序排序,但是,如果程式比較 2 位數字,則采用較小的字串。這適用于我的目的,它只是處理一致但任意命名的 .png 系列并將它們轉換為標準化格式。
bool alphaNumerSort(string i, string j){
string shorterStr;
(i.size() <= j.size()) ? shorterStr = i : shorterStr = j;
for (int k = 0; k < shorterStr.size(); k )
{
if (i[k] != j[k] && isdigit(i[k]) && isdigit(j[k]) ) {
if (i.size() == j.size()) {
return i[k] < j[k];
}
return (i.size() < j.size());
}
if (i[k] != j[k]) {
return i[k] < j[k];
}
}
return false;
//returns true if i is alphabetically before j
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/394591.html
上一篇:從集合中獲取隨機元素
