LoginSignup
2
2

More than 5 years have passed since last update.

string vectorの、sort and uniq (C++)

Last updated at Posted at 2019-01-13

string のソート、ユニークをしたい時は多々あります。
ここに、メモを兼ねて、基本中の基本のコードを上げておきます。
このコードは、$ cat - | sort | uniqと、同じ働きをします。

sortuniq.cpp
//
// String vector の、sort | uniq
//
#include    <iostream>
#include    <algorithm>
#include    <vector>
#include    <string>

using namespace std;

int main()
{
  vector<string> data;
  string  str;

    data.clear();   // データのクリア

  while(1) {      // EOF (Ctrl-D)まで読み込み
    cin >> str;
    if (cin.eof()) break;
    data.push_back(str); 
    }

  sort(data.begin(), data.end());                            // sort

  data.erase(unique(data.begin(), data.end()), data.end());  // uniq

    for(auto i:data) cout << i <<endl;   // 表示

    exit(0);
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2