c++ - Sorting a vector
问题描述
for ( fs::directory_iterator dir_itr2( out_folder ); dir_itr2 != end_iter; ++dir_itr2 )
{
cout << "Path del file da inserire nel vettore:" << dir_itr2->path().string().c_str() << endl;
final_path.push_back(dir_itr2->path().string().c_str());
}
sort( final_path.begin(), final_path.end() );
cout << "Vettore di path da aprire in ordine" << endl;
for(vector<const char *>::iterator fp_itr2=final_path.begin(); fp_itr2!=final_path.end(); ++fp_itr2)
{
cout << "Path: " << *fp_itr2 << endl;
}
Here I tried to put my path in a vector beacuse i need ordinated list, but the cout's output is this:
Path del file da inserire nel vettore:/srv/FD/super_tracker/tracks/180426163618363.txt
Path del file da inserire nel vettore:/srv/FD/super_tracker/tracks/180426163654027.txt
Path del file da inserire nel vettore:/srv/FD/super_tracker/tracks/180530150135770.txt
Path del file da inserire nel vettore:/srv/FD/super_tracker/tracks/180426163414599.txt
Path del file da inserire nel vettore:/srv/FD/super_tracker/tracks/180530150235481.txt
Path del file da inserire nel vettore:/srv/FD/super_tracker/tracks/180530150132796.txt
Path:
Path:
Path:
Path:
Path:
Path:
Thanks in advance.
解决方案
正如评论所说,不要使用char*
. 其次,您应该使用调试器。
您失败的原因sort()
是您正在根据指针指向的内存位置对指针进行排序,而不是使用指向的字符。
您可以使用谓词来告诉sort()
如何对对象进行排序:
sort(begin(final_path), end(final_path),
[](const char* a, const char *b) { return strcmp(a, b) < 0; }
);
但最好的当然是使用string
或直接path
作为向量元素的类型。
推荐阅读
- c - 在 C 中实现链表数组
- ssl - Kubevirt virtctl image-upload 给出“远程错误:tls:坏证书错误”
- authentication - SSH 密钥与令牌有何不同?
- angular - Angular : MSal 获取TokenSilent 问题
- javascript - 我的 javascript iife 动态加载脚本有什么问题
- algorithm - dec2base 具有独立的位/位计算
- excel - Excel 和时间字段比较
- sql - Microsoft Access:错误消息行大小太大
- jsxgraph - 在一组对象中,Segment 不移动
- c++ - 为什么这个并发代码比另一个(都是 C++)慢?