69,371
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <set>
#include <vector>
#include <bitset>
#include <list>
#include <iterator>
#include <functional>
#include <algorithm>
#include <numeric>
#include <cassert>
using namespace std;
class PartialStringLess : binary_function<string, string, bool>
{
public:
explicit PartialStringLess(size_t index) : index_(index) {}
result_type operator()(const first_argument_type &loperand, const second_argument_type &roperand)
{
assert(loperand.length() > index_);
assert(roperand.length() > index_);
return strcmp(loperand.c_str() + index_, roperand.c_str() + index_) < 0;
}
private:
size_t index_;
};
int main(void)
{
// Open file.
fstream file("C:\\Users\\Jim\\Documents\\test.txt");
if (!file)
{
cerr << "Can't open file!\n";
return 1;
}
// Read strings into vector.
istream_iterator<string> itBegin(file);
istream_iterator<string> itEnd;
vector<string> strings(itBegin, itEnd);
// Sort.
sort(strings.begin(), strings.end(), PartialStringLess(6));
// Write back to file.
file.clear();
file.seekp(0, fstream::beg);
ostream_iterator<string> oitBegin(file, "\n");
copy(strings.begin(), strings.end(), oitBegin);
return 0;
}
#include <process.h>
int main() {
//将文件c:\input.txt每行从第8列开始排序,结果保存到文件c:\output.txt中
system("sort /+8 c:\\input.txt /O c:\\output.txt");
return 0;
}