removeduplicates.cpp

来自「Thinking in C++ 2nd edition source code 」· C++ 代码 · 共 29 行

CPP
29
字号
//: C26:RemoveDuplicates.cpp
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Remove duplicate names from a mailing list
#include <vector>
#include <algorithm>
#include "../require.h"
#include "readLower.h"
using namespace std;

int main(int argc, char **argv) {
  requireArgs(argc,  3);
  vector<string> names;
  readLower(argv[1], names); 
  long before = names.size();
  // You must sort first for unique() to work:
  sort(names.begin(), names.end()); 
  // Remove adjacent duplicates:
  unique(names.begin(), names.end());
  long removed = before - names.size();
  ofstream out(argv[2]);
  assure(out, argv[2]);
  copy(names.begin(), names.end(),
       ostream_iterator<string>(out,"\n"));
  cout << removed << " names removed" << endl;
} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?