zoukankan      html  css  js  c++  java
  • list容器排除重复单词的程序

     1 #include<iostream> 
     2 #include<fstream> 
     3 #include<string>    
     4 #include<algorithm>
     5 #include<list>
     6 using namespace std;
     7 ifstream & open_file(ifstream & read, const string & file_name)
     8 {
     9     read.close();
    10     read.clear();
    11     read.open(file_name.c_str());
    12     return read;
    13 }
    14 
    15 int main()
    16 {
    17     string file_name;
    18     list < string > lis;
    19     string word;
    20     cout << "input the file_name: " << endl;
    21     cin >> file_name;
    22     cin.clear();
    23     ifstream read;
    24     open_file(read, file_name);
    25     while (read >> word) {
    26         lis.push_back(word);
    27     }
    28     cout << "the list is: " << endl;
    29     for (list < string >::iterator iter = lis.begin(); iter != lis.end();
    30          ++iter) {
    31         cout << *iter << " ";
    32     }
    33     cout << endl;
    34     lis.sort();
    35     lis.unique();
    36     cout << "the change list: " << endl;
    37     for (list < string >::iterator iter = lis.begin(); iter != lis.end();
    38          ++iter) {
    39         cout << *iter << " ";
    40     }
    41     return 0;
    42 }

     PS:

    • 1. list 容器不支持随机访问迭代器,不能运用泛型算法里的sort算法,用list特有算法sort
      1. 形式有两种: lis.sort();默认操作符<进行比较 lis.sort(comp);use function comp to compare elements
    • 2. unique function 至少支持前向迭代器,list容器可以用泛型算法的unique,也可以用特有算法unique,其作用是删除相邻重复的数据.
      • 二者差别: 泛型算法的unique将无重复的元素复制到序列的前端,从而覆盖相邻的重复元素,将重复元素移到序列的末端, 返回的迭代器指向超出无重复元素范围末端的下一位置。 list特有unique算法将重复元素删除出该容器
    • 3. sort函数将元素重新排列,如何将文本恢复原来的顺序呢??
  • 相关阅读:
    从零开始Windows环境下安装python+tensorflow
    Opencv调用深度学习模型
    python tensorflow 安装
    OpenCV3 Ref SVM : cv::ml::SVM Class Reference
    OpenCV3编程入门笔记(一)
    Tensorflow学习教程变量
    opencv3.0机器学习算法使用
    Tensorflow学习教程Fetch and Feed
    multilayer perceptrons, MLP)模型,CvANN_MLP。
    域的安装配置介绍
  • 原文地址:https://www.cnblogs.com/-lee/p/4254084.html
Copyright © 2011-2022 走看看