Hello, World!

日頃のメモ

c++でのちょっとした書き方の違い

先ほどのsortのプログラムで少し書き方が違うのがあったのでメモ

#include <iostream>
#include <list>
#include <string> //追加
#include <algorithm>  // std::sort, std::unique 追加
#include <vector>     // std::vector 追加
using namespace std;

//void print (list<string> &list1) {
void print (std::vector<string> &list1) {
  for (auto x: list1) {
    cout << x << " ";
  }
  cout << endl;
}

int main(int argc, char const* argv[])
{
  //list<string> list1 = { "ACC","ACC","CCW","CWA","WAC","CCT","CTT","TTT" };
  std::vector<string> list1 = { "ACC","CCW","CWA","ACC","WAC","CCT","CTT","TTT" };

  cout << "入力された文字" << endl;
  print(list1);

  //list1.sort();
  //list1.unique();
  std::sort(list1.begin(), list1.end());
  list1.erase(std::unique(list1.begin(), list1.end()), list1.end());
  cout << "After sort&unique list1: ";
  print(list1);

  return 0;
}