#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <boost/tr1/memory.hpp>
const char* FILENAME = "pass.txt";
const char* FILENAME2 = "pass2.txt";
bool Comp(const boost::shared_ptr<char>&s1, const boost::shared_ptr<char>& s2);
int main()
{
std::string buf;
std::vector<boost::shared_ptr<char> > vc;
std::time_t tt;
std::ifstream ifs(FILENAME);
if (!ifs)
std::exit(1);
/* 行数をカウントしながらvectorに読み込む */
while (true) {
std::getline(ifs, buf);
if (!ifs)
break;
buf.erase(buf.end() - 1); /* 行末の'\n'を取る */
char* p = new char[buf.length() + 1];
std::strcpy(p, buf.c_str());
boost::shared_ptr<char> sp(p);
vc.push_back(sp);
}
ifs.clear();
ifs.seekg(0, std::ios::beg);
std::cout << "文字列は " << vc.size() << " 行あります。" << std::endl;
ifs.close();
/* 文字列でソートする */
tt = time(0);
std::cout << "ソートを開始します。" << std::endl;
std::sort(vc.begin(), vc.end(), Comp);
std::cout << (std::time(0) - tt) << "秒掛かりました。" << std::endl;
std::ofstream ofs(FILENAME2);
if (!ofs)
std::exit(1);
std::cout << "ソート後の文字列を書き出しています。" << std::endl;
for (int i = 0; i < static_cast<int>(vc.size()); i++)
ofs << vc[i].get() << '\n';
ofs.close();
}
inline bool Comp(const boost::shared_ptr<char>&s1, const boost::shared_ptr<char>& s2)
{
return std::strcmp(s1.get(), s2.get()) < 0;
}