Professional C__ - Marc Gregoire [203]
: droppedStudents) {
allStudents.remove(str);
}
// done!
return allStudents;
}
Code snippet from StudentEnrollment\Enrollment.cpp
This example is using C++11 range-based for loops. If your compiler does not yet support those, you can implement the function as follows:
listgetTotalEnrollment(const vector >& courseStudents,const list& droppedStudents){
list allStudents;// Concatenate all the course lists onto the master list
for (vector >::const_iterator it = courseStudents.begin();it != courseStudents.end(); ++it) {
allStudents.insert(allStudents.end(), (*it).begin(), (*it).end());
}
// Sort the master list
allStudents.sort();
// Remove duplicate student names (those who are in multiple courses).
allStudents.unique();
// Remove students who are on the dropped list.
// Iterate through the drop list, calling remove on the
// master list for each student in the dropped list.
for (list::const_iterator it = droppedStudents.begin();it != droppedStudents.end(); ++it) {
allStudents.remove(*it);
}
// done!
return allStudents;
}
Code snippet from StudentEnrollment\Enrollment.cpp
array
The C++11 std::array class, defined in the header file, is similar to the vector except that it is of a fixed size; it cannot grow or shrink in size. The purpose of this is to allow an array to be allocated on the stack, rather than always demanding heap access as vector does. Just like vectors, arrays support random access iterators, and elements are stored in contiguous memory. It has support for front(), back(), at() and operator[]. It also supports a fill() method to fill the array with a specific element. Because it is fixed in size, it does not support push_back(), pop_back(), insert(), erase(), clear(), resize(), reserve() and capacity(). The following small example demonstrates how to use the array class. Note that the array declaration requires two template parameters; the first specifies the type of the elements, and the second specifies the fixed number of elements in the array.// Create array of 3 integers and initialize them
// with the given initializer_list using uniform initialization.
array arr = {9, 8, 7};// Output the size of the array.
cout << "Array size = " << arr.size() << endl;
// Output the contents of the array using iterators.
for (auto iter = arr.cbegin(); iter != arr.cend(); ++iter)
cout << *iter << endl;
cout << "Performing arr.fill(3)..." << endl;
// Use the fill method to change the contents of the array.
arr.fill(3);
// Output the contents using the range-based for loop.
for (auto& i : arr)
cout << i << endl;
Code snippet from std_array\std_array.cpp
The output of the preceding code is as follows:
Array size = 3
9
8
7
Performing arr.fill(3)...
3
3
3
forward_list
The forward_list introduced by C++11, defined in the header file, is similar to the list except that the forward_list is a singly linked list while the list is a doubly linked list. This means that the forward_list supports only forward iteration and because of this, ranges need to be specified differently compared to a list. If you want to modify any list, you need access to the element before the first element of interest. Since a forward_list does not have an iterator that supports going backward, there is no easy way to get to the preceding element. For this reason, ranges that will be modified, for example ranges supplied to erase and splice, must be open at the beginning. The begin() function seen earlier returns an iterator to the first element and thus can be used only to construct a range that is closed at the beginning. The forward_list class defines a before_begin() method, which returns an iterator that points to an imaginary element before the beginning of the list. You cannot dereference this iterator as it points to invalid data. However, incrementing this iterator by one will make it the same as the iterator returned by begin(); therefore, it can be used to make a range that is open at