String matching algorithm first post - match violence

String matching is an important element, the department string matching we are talking about exact match, that is, to find a substring matching position in the parent string.

For example: String Parent: EDGRNGIGEDEDGEDGLGDEDG, substring: EDG; we need to know the position of "EDG" string in the parent. First on the code:

1  // using a brute force method completed character string matching algorithm 
2 # the include " the iostream " 
. 3 #include " String " 
. 4 #include " Vector " 
. 5  the using  namespace STD;
 . 6 Vector < int > & BFmatch ( String &, String &, Vector < int > & );
 . 7  void showpos (Vector < int > & );
 . 8  int main ()
 . 9  {
 10      String ModelStr, SonStr;
. 11      Vector < int > POS;
 12 is      COUT << " Please enter a string to be matched: " ;
 13 is      CIN >> ModelStr;
 14      COUT << endl;
 15      COUT << " Please enter substring: " ;
 16      CIN >> SonStr;
 . 17      COUT << endl;
 18 is      BFmatch (ModelStr, SonStr, POS);
 . 19      showpos (POS);
 20 is      System ( " PAUSE " );
 21 is  }
 22 is Vector <int>& BFmatch(string & ModelStr, string & SonStr,vector<int>& pos)
23 {
24     for (int i = 0; i < ModelStr.size(); i++)
25     {
26         int k = 0;
27         for (int j = i; k < SonStr.size(); j++, k++)
28         {
29             if (SonStr[k] == ModelStr[j])
30                 continue;
31             else
32                 break;
33         }
34         if (k == SonStr.size())
35             pos.push_back(i);
36     }
37     return pos;
38 }
39 void ShowPos(vector<int>& pos)
40 {
41     if (pos.size() != 0)
42     {
43         cout << "the first position of MatchingStr:";
44         for (int i = 0; i < pos.size(); i++)
45         {
46             cout << pos[i] << "\t";
47         }
48         cout << endl;
49     }
50     else
51         cout << "no such string!" << endl;
52 }

Note that: the return type of the function BFmatch vector <int> &, return type is a reference type. We need to understand why there is a return to the vector <int> & type?

        We need to be clear is this: the return type of the function is: function return types! ! ! This sentence is very important. The return value is a variable pos, pos variable in the parameter type is passed inside; and pos type parameter is the vector <int> &. Why here vector <int> & type, you may say that we think that our intention is to return a vector storage location of the match, a larger space, space can be saved in this way, by way of transfer of value not save money. There is no other explanation of it?

        Indeed, suppose we do not consider this to save space. Not even pass pos variable, and let the function returns a local variable, the local variable will eventually be assigned to the main function pos, which of course also possible, but this one not only brings space overhead, more importantly program design concept.

         The design inside is: a function name and parameter, in terms of functions and interfaces of view, we want to include: name of the program, input interface, output interface . This is what we want to return the value of each function (that is, our output) is directly reflected in the rise of the function body. (Extensive use of this approach in opencv library). That is: function name, input and output terminals function is to describe a more scientific way of expression. And we want output terminal as a common interface, naturally can not use local variables, local variables only function in the present scope. Therefore, we need the use of a common interface way to define the output terminal, while the use of reference passed way is to implement common interface (of course there are other ways).

         Therefore, in the subsequent program design, consider the function name, input interface, output interface way to look at programming, is a more advanced way of abstraction.

 

Guess you like

Origin www.cnblogs.com/shaonianpi/p/11403960.html