(There are minor modifications.) ) using the STL algorithm library:
#include <algorithm>
#include <vector>//belong to STL Library Template Library
The people who write the library are separated from the standard C and C + + libraries
None of the template library files have a suffix
General #include <iostream.h>
#include in Template Gallery <iostream>
Tips: In GCC, in addition to the use of #include <vector>, but also to add a sentence unsing namespace std; In order to directly use VECTOR<INT> otherwise, use std::vector< int> xxx; it's OK.
To introduce a namespace
There are three methods:
1, using namespace std;
2, using Std::vector;
The first two methods are placed in the CPP file header, and H's head is also available.
3, upstairs like that, in every place with: std::vector<int> xx;
The C + + standard library uses a large number of templates. Although it is not necessary to encode using templates, it is important to use template code (which can save a lot of code). This section discusses some of the useful template types provided by the C + + standard libraries and STL. It is important to make full use of the STL, which is also the topic discussed in Chapter 7th.
1.1.1 String and basic_string<>
The string library is a very extensive library that uses templates to create a series of string types. When using strings, in the string and CString libraries, you should prioritize the string library, because CString is the char* string provided by the old C standard library.
Using string as a type is very simple, and it is actually a basic container type that specifically contains type char. In fact, a string is a template that is instantiated using the char type basic_string <char t>,basic_ string<> template can also be used to store wide character type wchar_t. This is useful when the ASCII character set does not fully represent the required character set, such as Chinese, Japanese, Finnish, or Korean languages. The Basic_string<> class represents a sequence of characters that contains all the common operations of the sequential container, as well as standard string operations, such as stitching.
Because type string and wstring are typedef that are used separately by basic_string<char> and basic_string<wchar_t>, you do not need to use templates directly basic_string <>.
The following examples illustrate some of the characteristics of the string type.
File TemplateString.cpp
String class to rewrite a sentence.
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
String sentence, words[10];
int pos = 0, Old_pos = 0, nwords, i = 0;
Sentence = "Eskimos have ways to";
Sentence + = "Describe snow";
while (Pos < Sentence.size ()) {
pos = Sentence.find (', old_pos);
Words[i].assign (sentence, Old_pos,
Pos-old_pos);
cout << words[i++] << Endl; Print words
Old_pos = pos + 1;
}
Nwords = i;
Sentence = "C + + programmers";
for (i = 1; i < nwords-1; ++i)
Sentence + = Words[i] + ';
Sentence + + "windows";
cout << sentence << Endl;
}
The string type is used to capture a single word from a sentence, and each word in the first sentence is separated by spaces. The position of the spaces is computed by the member function find (), and then the member function assign () selects a substring from the sentence. Finally, using overloaded functions operator= (), operator+= (), and operator+ () to assign values and stitching, create a new sentence.
Note that it is important to check the documentation for the local system, and different vendors provide their own implementation specifications.
Vector<> in the 1.1.2 Standard Template Library
We developed a generic array-like container vector<>. The complete std::vector<> is provided by the standard Template Library vector. In most cases, it is preferable to use the vector type instead of the base array type, because the vector checks whether the array is out of bounds, so it is more secure than the base array; vector can reassign array lengths and have a number of related standard methods, so it's more flexible than base arrays Vectors are represented as regular arrays, so it is easy to replace basic arrays. Here is an example:
#include <vector>
using namespace Std;
Template<class t>
t* Find (vector<t> data, T v)
{
int i;
for (i = 0; i < data.size (); i++)
if (data[i] = = v);
Return &data[i];
return 0; Indicates failure to find V
}
Note that the code above looks like a typical array, with the only exception being the size () method that returns the length of the vector.
1.1.3 Use complex<>
The template complex<> in the complex library provides a complex number type that is compatible with other numeric types. In the early C + + library, complex was not a template, it was just a type based on the following data description:
Class complex{
... methods
Private
Double x, y;
};
Now complex is described by the following template:
Template <class scalar>
Class complex{
... methods
Private
SCALAR x, y;
};
This allows the user to determine the precision of the base type as needed. Generally, these basic types are float, double, or long double. The following is a simple code for testing this type.
File Complex.cpp
#include <iostream>
#include <complex>
using namespace Std;
int main ()
{
complex<double> x (1,2.1), y;
cout << "x =" << x << Endl;
y = x + 1.0;
cout << "\ny =" << y << Endl;
if (x < y) not allowed-no standard definition
cout << "x less than Y" << Endl;
}
The complex type is important for scientists and engineers, and it shows how easy it is to extend C + + to new areas. For example, many scientists use FORTRAN90 programming because there are complex types in FORTRAN90. As a result, C + + has actually been able to replace a FORTRAN program that requires a plural type.
Note how the commented out rows are using the less-than operator. The operator is not defined for the complex type in the standard library, so the compilation fails when instantiating the template. If the reader has defined this operator himself, you can overload the operator and run the above code.
1.1.4 limits and other useful templates
The limits library describes the characteristics of various underlying types on the local system, and it provides the corresponding property information for all numeric types through the template class numberic_limits<>. In traditional C-language methods, properties for each data type are represented by a unique macro identifier, such as Int_max representing the maximum int value of the local system. In the template class numeric_limits<>, a static function, Max (), is defined to return the corresponding property value, such as Numeric_ Limits<int>::max (), which returns the maximum int value of the local system. The use of template classes can greatly reduce the number of symbol names used to describe the local system.
Here is an example of using the Numeric_limits<> function:
File Limits.cpp
#include <iostream>
#include <limits>
using namespace Std;
int main ()
{
cout << numeric_limits<char>::d igits << "char\n";
cout << numeric_limits<unsigned char>::d igits << "U char\n";
cout << numeric_limits<wchar_t>::d igits << "wchar_t\n";
cout << Numeric_limits<int>::max () << "Max Int\n";
cout << Numeric_limits<double>::max () << "Max Double" << Endl;
}
The digits field gives the number of digits of the current type.