Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
B.Eckel - Thinking in C++, Vol.2, 2nd edition.pdf
Скачиваний:
50
Добавлен:
08.05.2013
Размер:
2.09 Mб
Скачать

<< ns.occurrence << "]";

}

//Need this for sorting. Notice it only

//compares strings, not occurrences: friend bool

operator<(const NString& l, const NString& r) { return l.s < r.s;

}

//For sorting with greater<NString>:

friend bool

operator>(const NString& l, const NString& r) { return l.s > r.s;

}

// To get at the string directly:

operator const std::string&() const {return s;}

};

//Allocate static member object. Done here for

//brevity, but should actually be done in a

//separate cpp file:

NString::csmap NString::occurMap; #endif // NSTRING_H ///:~

In the constructors (one that takes a string, one that takes a char*), the simple-looking initialization occurrence(occurMap[s]++) performs all the work of maintaining and assigning the occurrence counts (see the demonstration of the map class in the previous chapter for more details).

To do an ordinary ascending sort, the only operator that’s necessary is

NString::operator<( ), however to sort in reverse order the operator>( ) is also provided so that the greater template can be used.

As this is just a demonstration class I am getting away with the convenience of putting the definition of the static member occurMap in the header file, but this will break down if the header file is included in more than one place, so you should normally relegate all static definitions to cpp files.

Filling & generating

These algorithms allow you to automatically fill a range with a particular value, or to generate a set of values for a particular range (these were introduced in the previous chapter). The “fill” functions insert a single value multiple times into the container, while the “generate” functions use an object called a generator (described earlier) to create the values to insert into the container.

Chapter 15: Multiple Inheritance

291

void fill(ForwardIterator first, ForwardIterator last, const T& value); void fill_n(OutputIterator first, Size n, const T& value);

fill( ) assigns value to every element in the range [first, last). fill_n( ) assigns value to n elements starting at first.

void generate(ForwardIterator first, ForwardIterator last, Generator gen); void generate_n(OutputIterator first, Size n, Generator gen);

generate( ) makes a call to gen( ) for each element in the range [first, last), presumably to produce a different value for each element. generate_n( ) calls gen( ) n times and assigns each result to n elements starting at first.

Example

The following example fills and generates into vectors. It also shows the use of print( ):

//: C05:FillGenerateTest.cpp

// Demonstrates "fill" and "generate" #include "Generators.h"

#include "PrintSequence.h" #include <vector>

#include <algorithm> #include <string> using namespace std;

int main() { vector<string> v1(5);

fill(v1.begin(), v1.end(), "howdy"); print(v1, "v1", " ");

vector<string> v2; fill_n(back_inserter(v2), 7, "bye"); print(v2.begin(), v2.end(), "v2"); vector<int> v3(10);

generate(v3.begin(), v3.end(), SkipGen(4,5)); print(v3, "v3", " ");

vector<int> v4; generate_n(back_inserter(v4),15, URandGen(30)); print(v4, "v4", " ");

} ///:~

A vector<string> is created with a pre-defined size. Since storage has already been created for all the string objects in the vector, fill( ) can use its assignment operator to assign a copy of “howdy” to each space in the vector. To print the result, the second form of print( ) is used which simply needs a container (you don’t have to give the first and last iterators). Also, the default newline separator is replaced with a space.

Chapter 15: Multiple Inheritance

292

Соседние файлы в предмете Численные методы
  • #
    08.05.20133.99 Mб22A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20135.91 Mб24B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136.09 Mб17D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20133.85 Mб15DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20131.84 Mб12E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu