#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>

using namespace std;
bool sortRule(const string&, const string&);

int main() {
  string s = "to be or not to be is a question";
  // Transfer string to stringstream
  istringstream ss(s);

  // Copy stringstream to vector
  vector<string> svec;
  copy(istream_iterator<string>(ss), istream_iterator<string>(), back_inserter(svec));

  // Sort vector by alphabetic order for unique() algorithm
  sort(svec.begin(), svec.end());

  // Unique vector
  vector<string>::iterator iter = unique(svec.begin(),svec.end());
  svec.erase(iter, svec.end());

  // Stable sort vector by size
  stable_sort(svec.begin(), svec.end(), sortRule);

  // Copy vector to cout
  copy(svec.begin(), svec.end(), ostream_iterator<string>(cout, "\n"));

  return 0;

bool sortRule(const string& s1, const string& s2) {
  return s1.size() < s2.size();


創作者 hedgezzz 的頭像


hedgezzz 發表在 痞客邦 留言(0) 人氣()