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

Basic sequences: vector, list & deque

If you take a step back from the STL containers you’ll see that there are really only two types of container: sequences (including vector, list, deque, stack, queue, and priority_queue) and associations (including set, multiset, map and multimap). The sequences keep the objects in whatever sequence that you establish (either by pushing the objects on the end or inserting them in the middle).

Since all the sequence containers have the same basic goal (to maintain your order) they seem relatively interchangeable. However, they differ in the efficiency of their operations, so if you are going to manipulate a sequence in a particular fashion you can choose the appropriate container for those types of manipulations. The “basic” sequence containers are vector, list and deque – these actually have fleshed-out implementations, while stack, queue and priority_queue are built on top of the basic sequences, and represent more specialized uses rather than differences in underlying structure (stack, for example, can be implemented using a deque, vector or list).

So far in this book I have been using vector as a catch-all container. This was acceptable because I’ve only used the simplest and safest operations, primarily push_back( ) and operator[ ]. However, when you start making more sophisticated uses of containers it becomes important to know more about their underlying implementations and behavior, so you can make the right choices (and, as you’ll see, stay out of trouble).

Basic sequence operations

Using a template, the following example shows the operations that all the basic sequences (vector, deque or list) support. As you shall learn in the sections on the specific sequence containers, not all of these operations make sense for each basic sequence, but they are supported.

//: C04:BasicSequenceOperations.cpp

//The operations available for all the

//basic sequence Containers.

#include <iostream> #include <vector> #include <deque> #include <list> using namespace std;

template<typename Container>

void print(Container& c, char* s = "") {

Chapter 15: Multiple Inheritance

169

cout << s << ":" << endl; if(c.empty()) {

cout << "(empty)" << endl; return;

}

typename Container::iterator it;

for(it = c.begin(); it != c.end(); it++) cout << *it << " ";

cout << endl;

cout << "size() " << c.size()

<<" max_size() "<< c.max_size()

<<" front() " << c.front()

<<" back() " << c.back() << endl;

}

template<typename ContainerOfInt> void basicOps(char* s) {

cout << "------- " << s << " -------" << endl; typedef ContainerOfInt Ci;

Ci c;

print(c, "c after default constructor"); Ci c2(10, 1); // 10 elements, values all 1 print(c2, "c2 after constructor(10,1)"); int ia[] = { 1, 3, 5, 7, 9 };

const int iasz = sizeof(ia)/sizeof(*ia);

//Initialize with begin & end iterators: Ci c3(ia, ia + iasz);

print(c3, "c3 after constructor(iter,iter)"); Ci c4(c2); // Copy-constructor

print(c4, "c4 after copy-constructor(c2)"); c = c2; // Assignment operator

print(c, "c after operator=c2");

c.assign(10, 2); // 10 elements, values all 2 print(c, "c after assign(10, 2)");

//Assign with begin & end iterators: c.assign(ia, ia + iasz);

print(c, "c after assign(iter, iter)");

cout << "c using reverse iterators:" << endl; typename Ci::reverse_iterator rit = c.rbegin(); while(rit != c.rend())

cout << *rit++ << " "; cout << endl; c.resize(4);

Chapter 15: Multiple Inheritance

170

print(c, "c after resize(4)"); c.push_back(47);

print(c, "c after push_back(47)"); c.pop_back();

print(c, "c after pop_back()"); typename Ci::iterator it = c.begin(); it++; it++;

c.insert(it, 74);

print(c, "c after insert(it, 74)"); it = c.begin();

it++;

c.insert(it, 3, 96);

print(c, "c after insert(it, 3, 96)"); it = c.begin();

it++;

c.insert(it, c3.begin(), c3.end()); print(c, "c after insert("

"it, c3.begin(), c3.end())"); it = c.begin();

it++;

c.erase(it);

print(c, "c after erase(it)");

typename Ci::iterator it2 = it = c.begin(); it++;

it2++; it2++; it2++; it2++; it2++; c.erase(it, it2);

print(c, "c after erase(it, it2)"); c.swap(c2);

print(c, "c after swap(c2)"); c.clear();

print(c, "c after clear()");

}

int main() {

basicOps<vector<int> >("vector"); basicOps<deque<int> >("deque"); basicOps<list<int> >("list");

} ///:~

The first function template, print( ), demonstrates the basic information you can get from any sequence container: whether it’s empty, its current size, the size of the largest possible container, the element at the beginning and the element at the end. You can also see that every container has begin( ) and end( ) methods that return iterators.

Chapter 15: Multiple Inheritance

171

Соседние файлы в предмете Численные методы
  • #
    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