-1
对于一个类项目,我们正在制作一个运行多个线程的grep类型程序,我们需要存储文件名和单词匹配的行号。结果需要由行号的文件名先进行排序,然后C++按主要次序排序,然后是次要条件
所以我要
namespace ultragrep
{
class File {
public:
int lineNumber;
std::string contents;
std::string fileName;
File(int ln, std::string lc, std::string fn) : lineNumber(ln), contents(lc), fileName(fn) {};
File() {};
~File(){};
string ln_toString() {
return "[" + std::to_string(lineNumber) + "]";
}
string contents_toString() {
return " \"" + contents + "\" ";
}
};
std::ostream& operator<<(std::ostream& out, const File& f) {
return out << "[" << f.fileName << "]...";
}
//operator < so sort works
bool operator < (File& lhs, File& rhs)
{
return lhs.fileName < rhs.fileName;
}
}
,当我所有的线程在我的主要完成()我有
sort(files.begin(), files.end());
for (ultragrep::File file : files)
{
cout << file << file.ln_toString() << file.contents_toString() << endl;
}
和这看起来会返回我期待的结果,但不能保证行号也是在一组结果中排序的。
示例的结果:
[file1.txt]...[1] "clock hello"
[file4.txt]...[1] "hello hello "
[file4.txt]...[2] "hello"
[file4.txt]...[3] "hello hello hello hello "
[file4.txt]...[5] "hello"
[file6.txt]...[3] "hello"
是存在的代码片段我可以添加到<过载,从而导致执行第二排序PARAM?
std :: string :: compare存在的原因是你可以有效地做这种事情。首先保存std :: string :: compare的结果。如果结果为零,则还需要测试次要标准。但是,第一次比较的非零结果意味着第二次不需要。就像'return(c = lhs.fileName.compare(rhs.fileName))<0 || c == 0 && lhs.line
JSF