zoukankan      html  css  js  c++  java
  • C++学习之字符串查询

    本博文主要探讨字符串的相关操作。

    问题描述:将一篇文本录入,实现查询功能。

    a):可以输入字符或者字符串,然后将包含他们的单词取出,并打印;(即返回一个容器)

    b):允许重复;

    c):如果查询词包含多项,则执行多次查询。例如:“hello world”,则先查询hello,后查询world。

    本程序待优化之处:

    1):每次查询都要从头到尾遍历一次容器。

    探讨如下:

    1):是否可以再readfile之后对容器进行排序;(因为程序只要求实现查询功能)。

    2):然后用二分查找进行查询。

    代码如下(不包括待优化项):

      1 #include <iostream>
      2 #include <string>
      3 #include <vector>
      4 #include <fstream>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include <sstream>
      9 #include <stdexcept>
     10 #define ERR_EXIT(m) 
     11     do { 
     12         perror(m);
     13         exit(EXIT_FAILURE);
     14     }while(0)
     15 using namespace std;
     16 
     17 void readfile( ifstream &in, vector<string> &vec);
     18 void toloweranderasepunct(string &str);
     19 vector<string> querystr( vector<string> &vec );
     20 
     21 int main(int argc, const char *argv[])
     22 {
     23     if(argc < 2)
     24     {
     25         perror("Usage: exe, filename");
     26         exit(EXIT_FAILURE);
     27     }
     28        
     29     vector<string> vec ;
     30     vector<string> dup ;
     31     ifstream in(argv[1]);
     32     if( !in )
     33         throw runtime_error("open file failure");
     34     
     35     readfile( in, vec );
     36     
     37 
     38     dup =  querystr( vec );
     39     cout << "from duplicate vector:" << endl ;
     40     
     41     for(vector<string>::iterator it = vec.begin(); 
     42         it != vec.end(); 
     43         ++it)
     44     {
     45         cout << *it << endl;    
     46     }
     47     
     48     in.close();
     49     return 0;
     50 }
     51 
     52 void readfile( ifstream &in, vector<string> &vec)
     53 {
     54     vec.clear();
     55     string s ;
     56     while(in >> s)
     57     {
     58         toloweranderasepunct( s );
     59         vec.push_back(s);
     60     }
     61 }
     62 
     63 void toloweranderasepunct( string &str)
     64 {
     65     string::iterator it = str.begin();
     66     while( it != str.end())
     67     {
     68         if(ispunct(*it))
     69         {
     70             it = str.erase(it);
     71         }else if(isupper(*it))
     72         {
     73             *it = tolower(*it);
     74             it++ ;
     75         }else
     76             it++ ;
     77     }
     78 }
     79 
     80 
     81 //veersion:search a line
     82 vector<string> querystr( vector<string> &vec)
     83 {
     84     string line ;
     85     vector<string> dup;
     86     while( getline( cin, line ) )
     87     {
     88         istringstream stream(line);
     89         string str ;
     90         while( stream >> str)
     91         {
     92             for(vector<string>::iterator it = vec.begin();
     93                     it != vec.end();
     94                     ++it)
     95             {
     96                 string::size_type pos = it->find(str);
     97                 if( pos!= string::npos )//success
     98                 {
     99                     cout << *it << endl ;
    100                     dup.push_back(*it);
    101                 }
    102             }
    103         }
    104     }
    105     return dup ;
    106 }
    107 
    108 /*
    109 //version1:search ch or string;
    110 void querystr( vector<string> &vec )//为什么不能用const
    111 {
    112     string str ;
    113     while( cin >> str)
    114     {
    115         for(vector<string>::iterator it = vec.begin();
    116             it != vec.end();
    117             ++it)
    118         {
    119             string::size_type pos = it->find(str);
    120             if( pos!= string::npos )//success
    121                 cout << *it << endl ;
    122         }
    123     }
    124 }
    125 
    126 */
  • 相关阅读:
    Permutations II
    N-Queens II
    Palindrome Number
    Minimum Path Sum
    JS的DOM操作2
    JS 的DOM操作
    函数概念
    JavaScript数组
    JavaScript循环及练习
    JS语言
  • 原文地址:https://www.cnblogs.com/xfxu/p/3985194.html
Copyright © 2011-2022 走看看