|
 |
 |
[an error occurred while processing this directive]
- 2. Using the mismatch algorithm
In the previous example, you were not interested in finding the difference between the two ranges. The knowledge that the incoming range was the same or was different from the pattern was sufficient. Now you want to know which element makes this difference. For that reason, you are going to change the equal algorithm to the mismatch algorithm.
// equal_a.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int> pattern;
vector <int> inData;
vector <int>::iterator i;
pattern.push_back(12);
pattern.push_back(25);
pattern.push_back(33);
pattern.push_back(49);
inData.push_back(25);
inData.push_back(33);
inData.push_back(48);
i = pattern.begin();
i++;
pair<int*, int*> result;
result= mismatch(i, pattern.end(), inData.begin());
if (equal(i, pattern.end(), inData.begin()))
cout << The ranges are equal << endl;
else
cout << The different values are: <<
*(result.first) << and <<
*(result.second) << endl;
return 0;
}
This example was written using the Microsoft Visual C++ version 5 compiler. If you use the Borland C++ 5 compiler, you have to include the <utility> header file for pair definition.
Running the application now will show
The different values are 48 and 49
The syntax of the mismatch algorithms is interesting. In order to execute it, you wrote the two lines:
pair<int*, int*> result;
result= mismatch(i, pattern.end(), inData.begin());
The first line defines a pair of two variables of the type int*. In general, the pair is heterogeneousthe types of the variables shouldnt be the same. A pair is similar to a structure: the first member in the pair is referenced as result.first, and the second member is referenced as result.second, where result is the name of the pair.
pair allows you to return the two values. The mismatch algorithm also returns two values: the first occurrence of the different elements in the ranges. If no such difference exists, the first member of the mismatch algorithm result points after the end of the first range.
- 3. Comparison of the two cases
The equal and mismatch algorithms solve the same problem. Both algorithms compare two ranges element by element. Using the equal algorithm can be simpler because it does not need to define a pair of resulting values. The mismatch algorithm returns the different values that might be necessary for later use in the program.
Comments
Matching a range of data against another range of data is a very common task in programming. The equal and mismatch algorithms provide this matching capability.
9.5 Search for a sequence of values in a container?
Problem
For text processing applications, user interface design, and other data parsing, I have to search for sub-sequences of data in sequences such as texts. I already know how I can search for a word in a sequence of characters. Can I search for a sequence of words in a text if the texts are vectors of words?
Technique
The STL provides the search algorithm that searches for a range of data in another range of data. The data should be of the same type. This How-To describes an example that searches a sequence of words. The program keeps the words in the vector container with the elements of the string type.
Another example extends the algorithm capabilities. The example shows how you can use the search algorithm to compare the elements by applying a special function. This How-To will search for a sub-sequence of words that start with the same letters as the specified sequence of words.
Steps
- 1. Applying the search algorithm
In this How-To, you will create a program that searches for a word combination in a quote from Oscar Wilde: All art is quite useless. The first search will be done for the sequence quite useless.
// search.cpp
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <string> MainText;
vector <string> TempString;
vector <string>::iterator i;
MainText.push_back(All);
MainText.push_back(art);
MainText.push_back(is);
MainText.push_back(quite);
MainText.push_back(useless);
TempString.push_back(quite);
TempString.push_back(useless);
i = search (MainText.begin(), MainText.end(),
TempString.begin(), TempString.end());
if (i == MainText.end())
cout << The substring is not found << endl;
else
cout << The substring is found << endl;
return 0;
}
The programming example given here is much simpler than a real program. To maintain the sentence All art is quite useless, the MainText vector is defined and filled in with the push_back function. The word combination quite useless is being kept in another vector, TempString. Both vectors have elements of the string data type; therefore, the following header files were included:
#include <vector>
#include <iostream>
This example was created using Microsoft Visual C++ version 5. If you use other compilers or older versions of the Microsoft compiler, you might have to include the <algorithm.h>, <string.h>, <vector.h>, and <iostream.h> header files.
|