|
 |
 |
[an error occurred while processing this directive]
The search algorithm in its simplest version takes four arguments. The first two arguments specify the data range in the main sequence. In this example, you want to search from the very beginning to the very end of MainText. The second pair of the arguments specifies the range of the sequence to search for.
The result of the search algorithm is an iterator that points to the beginning of a sub-sequence in the main sequence that is equal to the second sequence. In this example, the i iterator points to the element of the main sequence that contains the word quite. If the search algorithm fails, the result would point after the end of the main sequence.
If you run the program, it will show
The substring is found
on the computer screen.
- 2. Applying the search algorithm with a special comparison function
A simple search uses the equality operator or an overloaded equality operator to compare the sequences. If you want to make the search more sophisticated, you can specify another binary function for the comparison.
This case, you search for the word combination that starts with the same letters as the given word combination. In other words, the equality operator will be replaced with the following function:
#include <functional>
class WordCompare:binary_function <string, string, bool>
{
public:
bool operator()(const string& a, const string& b) {
if (a.at(0) == b.at(0))
return true;
else
return false;
}
};
For binary_function support, you have to include the <functional> header file.
Change the word combination you want to use as a search pattern. Search for the sub-sequence that starts with the same letters as the word combination quite unique.
// search.cpp
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
class WordCompare:binary_function <string, string, bool>
{
public:
bool operator()(const string& a, const string& b) {
if (a.at(0) == b.at(0))
return true;
else
return false;
}
};
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(unique);
i = search (MainText.begin(), MainText.end(),
TempString.begin(), TempString.end(), WordCompare());
if (i == MainText.end())
cout << The substring is not found << endl;
else
cout << The substring is found << endl;
return 0;
}
If the program is run, the output will be
The substring is found
because the sentence All art is quite useless has a word combination quite useless that starts with the same letters as quite unique.
The search function in this example accepts five arguments. The last one is the function object that defines the comparison procedure.
Comments
The search algorithm is a powerful tool that can be used to search within data containers. The search procedure can be expanded by the usage of a function object that specifies the comparison procedure.
9.6 Accumulate all container elements and create a sequence of accumulated sums?
Problem
I want to accumulate data for my accounting program as a simple sum and as an accumulated sum. In other words, I want to create a sequence of partial sums in the given sequence. For another program, I want to be able to calculate products of the given values and accumulate them. Is there a standard function in the STL that helps me perform this task?
Technique
The STL has a few algorithms that can accumulate data. The algorithms discussed in this section are the accumulate and the partial_sum algorithms. The modifications of the algorithms can use functions other than addition, which increases their applicability.
The syntax of the algorithms is simple enough to allow a user to implement them intuitively. This How-To discusses a few examples of the implementation of these two algorithms.
Steps
- 1. Accumulating data using addition
The first example creates a sum for a set of transactions. The transactions are represented with a vector container. The program simply calculates the sum and shows it on the screen.
// accum.cpp
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
using namespace std;
int main()
{
vector <float> Transaction;
float InitValue= 0.0;
float ResultSum;
Transaction.push_back(18.56);
Transaction.push_back(-100.00);
Transaction.push_back(123.01);
Transaction.push_back(7610.23);
Transaction.push_back(-507.65);
ResultSum = accumulate (Transaction.begin(),
⇒Transaction.end(), InitValue);
cout << The balance of the transations is
<< ResultSum << endl;
return 0;
}
The program needs to include a few header files. <iostream> for the cout, <vector> for the vector container support, and <algorithm> for the usage of STL algorithms are files that you already used. However, in this example you also need the <numeric> header file that supports numeric algorithms, such as the accumulate algorithm. The compiler directive
using namespace std;
is the usual addition to these standard header files.
The Transaction vector of float values will contain all transaction amounts for your calculations. Initialize the vector using the push_back member function of the vector container.
|