Overview. Part II: Containers and Algorithms. Overview. Overview

Size: px
Start display at page:

Download "Overview. Part II: Containers and Algorithms. Overview. Overview"

Transcription

1 Part II: Containers and Algorithms Overview Sequential Containers Associative Containers Generic Algorithms C++ is about efficient programming with abstractions. The Standard Library is a good example: The library defines a number of container classes and a family of generic algorithms that let us write programs that are succinct, abstract, and efficient The library worries about bookkeeping details in particular, taking care of memory management so that our programs can worry about the actual problems we need to solve. 1 2 Overview We introduced the vector container type. We'll learn more in Chapter 9 about vector and the other sequential container types provided by the library We'll also cover more operations provided by the string type We can think of a string as a special kind of container that contains only characters The string type supports many, but not all, of the container operations. Overview The library also defines several associative containers Elements in an associative container are ordered by key rather than sequentially The associative containers share many operations with the sequential containers and also define operations that are specific to the associative containers 3 4 1

2 Overview Overview Chapter 11 introduces the generic algorithms The algorithms typically operate on a range of elements from a container or other sequence The algorithms library offers efficient implementations of various classical algorithms, such as searching, sorting, and other common tasks copy algorithm copies elements from one sequence to another find looks for a given element; and so on. The algorithms are generic in two ways: they can be applied to different kinds of containers those containers may contain elements of most types. The library is designed so that the container types provide a common interface: If two containers offer a similar operation, then that operation will be defined identically for both containers For example, all the containers have an operation to return the number of elements in the container. All the containers name that operation size, and they all define a type named size_type that is the type of the value returned by size Similarly, the algorithms have a consistent interface. For example, most algorithms operate on a range of elements specified by a pair of iterators. 5 6 Overview Because the container operations and algorithms are defined consistently, learning the library becomes easier Once you understand how an operation works, you can apply that same operation to other containers More importantly, this commonality of interface leads to more flexible programs It is often possible to take a program written to use one container type and change it to use a different container without having to rewrite code As we'll see, the containers offer different performance tradeoffs, and the ability to change container types can be valuable when fine-tuning the performance of a system. 7 Chapter 9: IO Library Defining a Sequential Container Iterators and Iterator Ranges Sequence Container Operations How a vector Grows Deciding Which Container to Use strings Revisited Container Adaptors 8 2

3 Overview Elements in a sequential container are stored and accessed by position. The library also defines several associative containers, which hold elements whose order depends on a key (next chapter) The container classes share a common interface This fact makes the library easier to learn; what we learn about one type applies to another Each container type offers a different set of time and functionality tradeoffs. Often a program using one type can be fine-tuned by substituting another container without changing our code beyond the need to change type declarations. 9 Overview A container holds a collection of objects of a specified type We've used one kind of container already: the library vector type. It is a sequential container. It holds a collection of elements of a single type, making it a container Those elements are stored and accessed by position, making it a sequential container The order of elements in a sequential container is independent of the value of the elements Instead, the order is determined by the order in which elements are added to the container. 10 Overview The library defines three kinds of sequential containers: vector list deque ("double-ended queue, pronounced "deck") These types differ in how elements are accessed and the relative run-time cost of adding or removing elements The library also provides three container adaptors Effectively, an adaptor adapts an underlying container type by defining a new interface in terms of the operations provided by the original type The sequential container adaptors are stack, queue, and priority_queue. 11 Overview Containers define only a small number of operations Many additional operations are provided by the algorithms library For those operations that are defined by the containers, the library imposes a common interface The containers vary as to which operations they provide, but if two containers provide the same operation, then the interface (name and number of arguments) will be the same for both container types 12 3

4 Overview Sequential Container Types The set of operations on the container types form a kind of hierarchy: Some operations are supported by all container types. Other operations are common to only the sequential or only the associative containers. Still others are common to only a subset of either the sequential or associative containers. vector supports fast random access list supports fast insertion/deletion Deque double-ended queue Sequential Container Adaptors stack last in/first out stack queue first in/first out queue priority_queue priority-managed queue Defining a Sequential Container To define a container object, we must include its associated header file, which is one of #include <vector> #include <list> #include <deque> Each of the containers is a class template To define a particular kind of container, we name the container followed by angle brackets that enclose the type of the elements the container will hold: vector<string> svec; // empty vector that can hold strings list<int> ilist; // empty list that can hold ints deque<sales_item> items; // empty deque that holds Sales_items

5 Initializing Container Elements Each container defines a default constructor that creates an empty container of the specified type Recall that a default constructor takes no arguments In addition to defining a default constructor, each container type also supports constructors that allow us to specify initial element values Intializing a Container as a Copy of Another Container When we initialize a sequential container using any constructor other than the default constructor, we must indicate how many elements the container will have We must also supply initial values for those elements One way to specify both the size and element values is to initialize a new container as a copy of an existing container of the same type Intializing a Container as a Copy of Another Container vector<int> ivec; vector<int> ivec2(ivec); // ok: ivec is vector<int> list<int> ilist(ivec); // error: ivec is not list<int> vector<double> dvec(ivec); // error: ivec holds int not double Initializing as a Copy of a Range of Elements Although we cannot copy the elements from one kind of container to another directly, we can do so indirectly by passing a pair of iterators When we use iterators, there is no requirement that the container types be identical The element types in the containers can differ as long as they are compatible It must be possible to convert the element we copy into the type held by the container we are constructing

6 Initializing as a Copy of a Range of Elements The iterators denote a range of elements that we want to copy These elements are used to initialize the elements of the new container The iterators mark the first and one past the last element to be copied We can use this form of initialization to copy a container that we could not copy directly More importantly, we can use it to copy only a subsequence of the other container: Initializing as a Copy of a Range of Elements // initialize slist with copy of each element of svec list<string> slist(svec.begin(), svec.end()); // find midpoint in the vector vector<string>::iterator mid = svec.begin() + svec.size()/2; // initialize front with first half of svec: The elements up to but not // including *mid deque<string> front(svec.begin(), mid); // initialize back with second half of svec: The elements *mid // through end of svec deque<string> back(mid, svec.end()); Initializing as a Copy of a Range of Elements Recall that pointers are iterators, so it should not be surprising that we can initialize a container from a pair of pointers into a built-in array char *words[] = {"stately", "plump", "buck", "mulligan"}; // calculate how many elements in words size_t words_size = sizeof(words)/sizeof(char *); Allocating and Initializing a Specified Number of Elements When creating a sequential container, we may specify an explicit size and an (optional) initializer to use for the elements The size can be either a constant or non-constant expression The element initializer must be a valid value that can be used to initialize an object of the element type const list<int>::size_type list_size = 64; list<string> slist(list_size, "eh?"); // 64 strings, each is eh? // use entire array to initialize words2 list<string> words2(words, words + words_size);

7 Allocating and Initializing a Specified Number of Elements As an alternative to specifying the number of elements and an element initializer, we can also specify only the size list<int> ilist(list_size); // 64 elements, each initialized to 0 // svec has as many elements as the return value from // get_word_count extern unsigned get_word_count(const string &file_name); Allocating and Initializing a Specified Number of Elements When we do not supply an element initializer, the library generates a value-initialized one for us To use this form of initialization, the element type must either be a built-in or compound type or be a class type that has a default constructor If the element type does not have a default constructor, then an explicit element initializer must be specified. vector<string> svec(get_word_count("chimera")); Constraints on Types that a Container Can Hold While most types can be used as the element type of a container, there are two constraints that element types must meet: The element type must support assignment. We must be able to copy objects of the element type Most types meet these minimal element type requirements All of the built-in or compound types, with the exception of references, can be used as the element type References do not support assignment in its ordinary meaning, so we cannot have containers of references. Constraints on Types that a Container Can Hold Explain the following initializations. Indicate if any are in error, and if so, why. int ia[7] = { 0, 1, 1, 2, 3, 5, 8 }; string sa[6] = { "Fort Sumter", "Manassas", "Perryville", "Vicksburg", "Meridian", "Chancellorsville" }; (a) vector<string> svec(sa, sa+6); (b) list<int> ilist( ia+4, ia+6); (c) vector<int> ivec(ia, ia+8); (d) list<string> slist(sa+6, sa);

8 Container Operations May Impose Additional Requirements The requirement to support copy and assignment is the minimal requirement on element types In addition, some container operations impose additional requirements on the element type If the element type doesn't support the additional requirement, then we cannot perform that operation: We can define a container of that type but may not use that particular operation Container Operations May Impose Additional Requirements One example of an operation that imposes a type constraint is the constructors that take a single initializer that specifies the size of the container If our container holds objects of a class type, then we can use this constructor only if the element type has a default constructor Most types do have a default constructor, although there are some classes that do not Container Operations May Impose Additional Requirements As an example, assume that Foo is a class that does not define a default constructor but that does have a constructor that takes an int argument vector<foo> empty; // ok: no need for default constructor vector<foo> bad(10); // error: no default constructor for Foo vector<foo> ok(10, 1); // ok: each element initialized to 1 Containers of Containers Because the containers meet the constraints on element types, we can define a container whose element type is itself a container type For example, we might define lines as a vector whose elements are a vector of strings: vector< vector<string> > lines; // vector of vectors Note the spacing used when specifying a container element type as a container: vector< vector<string>> lines; // error: >> treated as shift operator

9 Iterators and Iterator Ranges Iterators and Iterator Ranges The constructors that take a pair of iterators are an example of a common form used extensively throughout the library Each of the container types has several companion iterator types Like the containers, the iterators have a common interface If an iterator provides an operation, then the operation is supported in the same way for each iterator that supplies that operation For example, all the container iterators let us read an element from a container, and they all do so by providing the dereference operator Similarly, they all provide increment and decrement operators to allow us to go from one element to the next Common Iterator Operations *iter return a reference to the element referred to by the iterator iter iter->mem dereference iter and fetch the member named mem from the underlying element ++iter iter++ increment iter to refer to the next element in the container Common Iterator Operations --iter iter decrement iter to refer to the previous element in the container iter1 == iter2 iter1!= iter2 compare two iterators for equality (inequality). Two iterators are equal if they refer to the same element of the same container or if they are the off-the-end iterator for the same container

10 Iterators on vector and deque Support Additional Operations iter + n, iter - n Adding (subtracting) an integral value n to (from) an iterator yields an iterator that many elements forward (backward) within the container. The resulting iterator must refer to an element in the container or one past the end of the container Iterators on vector and deque Support Additional Operations iter1 += iter2, iter1 -= iter2 Compound-assignment versions of iterator addition and subtraction. Assigns the value of adding or subtracting iter1 and iter2 into iter Iterators on vector and deque Support Additional Operations iter1 - iter2 Subtracting two iterators yields the number that when added to the right-hand iterator yields the lefthand iterator. The iterators must refer to elements in the same container or one past the end of the container Iterators on vector and deque Support Additional Operations Supported only for vector and deque >, >=, <, <= Relational operators on iterators. One iterator is less than another if it refers to an element whose position in the container is ahead of the one referred to by the other iterator. The iterators must refer to elements in the same container or one past the end of the container

11 Iterators on vector and deque Support Additional Operations The reason that only vector and deque support the relational operators is that only vector and deque offer fast, random access to their element These containers are guaranteed to let us efficiently jump directly to an element given its position in the container Because these containers support random access by position, it is possible for their iterators to efficiently implement the arithmetic and relational operations Iterators on vector and deque Support Additional Operations For example, we could calculate the midpoint of a vector as follows: vector<int>::iterator iter = vec.begin() + vec.size()/2; // copy elements from vec into ilist list<int> ilist(vec.begin(), vec.end()); ilist.begin() + ilist.size()/2; // error: no addition on list iterators Iterator Ranges Iterator Ranges The concept of an iterator range is fundamental to the standard library An iterator range is denoted by a pair of iterators that refer to two elements, or to one past the last element, in the same container These two iterators, often referred to as first and last, or beg and end, mark a range of elements from the container. Although the names last and end are common, they are a bit misleading The second iterator never refers to the last element of the range. Instead, it refers to a point one past the last element This element range is called a left-inclusive interval. The standard notation for such a range is // to be read as: includes first and each element up to but not // including last [ first, last )

12 Requirements on Iterators Forming an Iterator Range Two iterators, first and last, form an iterator range, if They refer to elements of, or one-past-the-end of, the same container. If the iterators are not equal, then it must be possible to reach last by repeatedly incrementing first. In other words, last must not precede first in the container The compiler cannot itself enforce these requirements. It does not know to which container an iterator is bound, nor does it know how many elements are in a container. Failing to meet these requirements results in undefined run-time behavior. Programming Implications of Using Left- Inclusive Ranges The library uses left-inclusive ranges because such ranges have two convenient properties Assuming first and last denote a valid iterator range, then When first equals last, the range is empty. When first is not equal to last, there is at least one element in the range, and first refers to the first element in that range. Moreover, we can advance first by incrementing it some number of times until first == last Some Container Operations Invalidate Iterators some container operations change the internal state of a container or cause the elements in the container to be moved Such operations invalidate all iterators that refer to the elements that are moved and may invalidate other iterators as well Using an invalidated iterator is undefined, and likely to lead to the same kinds of problems as using a dangling pointer. Some Container Operations Invalidate Iterators each container defines one or more erase functions. These functions remove elements from the container. Any iterator that refers to an element that is removed has an invalid value the iterator was positioned on an element that no longer exists within the container When writing programs that use iterators, we must be aware of which operations can invalidate the iterators. It is a serious run-time error to use an iterator that has been invalidated

13 Sequence Container Operations Container Typedefs Each sequential container defines a set of useful typedefs and supports operations that let us Add elements to the container Delete elements from the container Determine the size of the container Fetch the first and last elements from the container, if any size_type Unsigned integral type large enough to hold size of largest possible container of this container type iterator Type of the iterator for this container type const_iterator type of the iterator that can read but not write the elements reverse_iterator Iterator that addresses elements in reverse order Container Typedefs Container Typedefs difference_type Signed integral type large enough to hold the difference, which might be negative, between two iterators value_type Element type reference Element's lvalue type; synonym for value_type& const_reference element's const lvalue type; same as const value_type& a reverse iterator is an iterator that goes backward through a container and inverts the iterator operations For example, saying ++ on a reverse iterator yields the previous element in the container The last three types in Table 9.5 on the facing page let us use the type of the elements stored in a container without directly knowing what that type is If we need the element type, we refer to the container's value_type If we need a reference to that type, we use reference or const_reference

14 Container Typedefs begin and end Members // iter is the iterator type defined by list<string> list<string>::iterator iter; // cnt is the difference_type type defined by vector<int> vector<int>::difference_type cnt; The begin and end operations yield iterators that refer to the first and one past the last element in the container These iterators are most often used to form an iterator range that encompasses all the elements in the container c.begin() c.end() c.rbegin() c.rend() Adding Elements to a Sequential Container Every sequential container supports push_back, which appends an element to the back of the container // read from standard input putting each word onto the end string text_word; while (cin >> text_word) container.push_back(text_word); Adding Elements to a Sequential Container In addition to push_back, the list and deque containers support an analogous operation named push_front. This operation inserts a new element at the front of the container The call to push_back creates a new element at the end of container, increasing the size of container by one

15 Adding Elements to a Sequential Container list<int> ilist; // add elements at the end of ilist for (size_t ix = 0; ix!= 4; ++ix) ilist.push_back(ix); Alternatively, we could use push_front // add elements to the start of ilist for (size_t ix = 0; ix!= 4; ++ix) ilist.push_front(ix); After executing both loops, ilist holds the sequence 3,2,1,0,0,1,2,3. 57 Operations that Add Elements to a Sequential Container Valid only for list or deque c.insert(p,t) Inserts element with value t before the element referred to by iterator p. Returns an iterator referring to the element that was added c.insert(p,n,t) Inserts n elements with value t before the element referred to by iterator p. Returns void c.insert(p,b,e) Inserts elements in the range denoted by iterators b and e before the element referred to by iterator p. Returns void. 58 Adding Elements at a Specified Point in the Container The push_back and push_front operations provide convenient ways to insert a single element at the end or beginning of a sequential container More generally, insert allows us to insert elements at any particular point in the container There are three versions of insert The first takes an iterator and an element value. The iterator refers to the position at which to insert the value. We could use this version of insert to insert an element at the beginning of a container Adding Elements at a Specified Point in the Container vector<string> svec; list<string> slist; string spouse("beth"); // equivalent to calling slist.push_front (spouse); slist.insert(slist.begin(), spouse); // no push_front on vector but we can insert before begin() svec.insert(svec.begin(), spouse);

16 Adding Elements at a Specified Point in the Container insert returns an iterator referring to the newly inserted element. We could use the return value to repeatedly insert elements at a specified position in the container list<string> lst; list<string>::iterator iter = lst.begin(); while (cin >> word) iter = lst.insert(iter, word); // same as calling push_front Inserting a Range of Elements A second form of insert adds a specified number of identical elements at an indicated position svec.insert(svec.end(), 10, "Anna"); This code inserts ten elements at the end of svec and initializes each of those elements to the string "Anna" Inserting a Range of Elements The final form of insert adds a range of elements denoted by an iterator pair into the container For example, given the following array of strings string sarray[4] = {"quasi", "simba", "frollo", "scar"}; we can insert all or a subset of the array elements into our list of strings: // insert all the elements in sarray at end of slist slist.insert(slist.end(), sarray, sarray+4); list<string>::iterator slist_iter = slist.begin(); // insert last two elements of sarray before slist_iter slist.insert(slist_iter, sarray+2, sarray+4); 63 Inserting Elements Can Invalidate Iterators Adding elements to a vector can cause the entire container to be relocated If the container is relocated, then all iterators into the container are invalidated Even if the vector does not have to be relocated, any iterator to an element after the one inserted is invalidated. Iterators may be invalidated after doing any insert or push operation on a vector or deque. When writing loops that insert elements into a vector or a deque, the program must ensure that the iterator is refreshed on each trip through the loop

17 Avoid Storing the Iterator Returned from end When we add elements to a vector or deque, some or all of the iterators may be invalidated It is safest to assume that all iterators are invalid This advice is particularly true for the iterator returned by end. That iterator is always invalidated by any insertion anywhere in the container Avoid Storing the Iterator Returned from end As an example, consider a loop that reads each element, processes it and adds a new element following the original We want the loop to process each original element We'll use the form of insert that takes a single value and returns an iterator to the element that was just inserted After each insertion, we'll increment the iterator that is returned so that the loop is positioned to operate on the next original element Avoid Storing the Iterator Returned from end vector<int>::iterator first = v.begin(), last = v.end(); // cache end iterator // diaster: behavior of this loop is undefined while (first!= last) { // do some processing // insert new value and reassign first, which otherwise // would be invalid first = v.insert(first, 42); ++first; // advance first just past the element we added } Adding an element invalidates the iterator stored in last. That iterator neither refers to an element in v nor any longer refers to Avoid Storing the Iterator Returned from end one past the last element in v Rather than storing the end iterator, we must recompute it after each insertion: // safer: recalculate end on each trip whenever the loop // adds/erases elements while (first!= v.end()) { // do some processing first = v.insert(first, 42); // insert new value ++first; // advance first just past the element we added } 17

18 Relational Operators Relational Operators Relational operators can be used to compare two containers The containers must be the same kind of container and must hold elements of the same type We can compare a vector<int> only with another vector<int>. We cannot compare a vector<int> with a list<int> or a vector<double>. Comparing two containers is based on a pairwise comparison of the elements of the containers The comparison uses the same relational operator as defined by the element type Comparing two containers using!= uses the!= operator for the element type Relational Operators Relational Operators These operators work similarly to the string relationals If both containers are the same size and all the elements are equal, then the two containers are equal; otherwise, they are unequal. If the containers have different sizes but every element of the shorter one is equal to the corresponding element of the longer one, then the shorter one is considered to be less than the other. If neither container is an initial subsequence of the other, then the comparison depends on comparing the first unequal elements Examples /* ivec1: ivec2: ivec3: ivec4: ivec5: */ // ivec1 and ivec2 differ at element[0]: ivec1 greater than ivec2 ivec1 < ivec2 // false ivec2 < ivec1 // true

19 Relational Operators Relational Operators Examples /* ivec1: ivec2: ivec3: ivec4: ivec5: */ // ivec1 and ivec3 differ at element[2]: ivec1 less than ivec3 ivec1 < ivec3 // true Examples /* ivec1: ivec2: ivec3: ivec4: ivec5: */ // all elements equal, but ivec4 has fewer elements, so ivec1 is greater than ivec4 ivec1 < ivec4 // false Relational Operators Relational Operators Examples /* ivec1: ivec2: ivec3: ivec4: ivec5: */ ivec1 == ivec5 // true; each element equal and same number of //elements ivec1 == ivec4 // false; ivec4 has fewer elements than ivec1 ivec1!= ivec4 // true; ivec4 has fewer elements than ivec1 We can compare two containers only if the same relational operator defined for the element types Each container relational operator executes by comparing pairs of elements from the two containers: ivec1 < ivec2 Assuming ivec1 and ivec2 are vector<int>, then this comparison uses the built-in int less-than operator If the vectors held strings, then the string less-than operator would be used

20 Relational Operators Container Size Operations If the vectors held objects of the Sales_item type that we used, then the comparison would be illegal We did not define the relational operators for Sales_item If we have two containers of Sales_items, we could not compare them Each container type supports four size-related operations size() returns the number of elements in the container; empty() returns a bool that is true if size is zero and false otherwise vector<sales_item> storea; vector<sales_item> storeb; if (storea < storeb) // error: Sales_item has no less-than operator Container Size Operations Container Size Operations The resize operation changes the number of elements in the container If the current size is greater than the new size, then elements are deleted from the back of the container. If the current size is less than the new size, then elements are added to the back of the container list<int> ilist(10, 42); // 10 ints: each has value 42 ilist.resize(15); // adds 5 elements of value 0 to back of ilist ilist.resize(25, -1); // adds 10 elements of value -1 to back ilist.resize(5); // erases 20 elements from the back of ilist resize can invalidate iterators. A resize operation on a vector or deque potentially invalidates all iterators

21 Erasing Elements Removing the First or Last Element Recall that there is both a general insert operation that inserts anywhere in the container and specific push_front and push_back operations to add elements only at the front or back Similarly, there is a general erase and specific pop_front and pop_back operations to remove elements while (!ilist.empty()) { process(ilist.front()); // do something with the current top of ilist ilist.pop_front(); // done; remove first element } Removing an Element From within the Container The more general way to remove an element, or range of elements, is through erase There are two versions of erase: delete a single element referred to by an iterator delete a range of elements marked by a pair of iterators. Both forms of erase return an iterator referring to the location after the element or range that was removed Removing an Element From within the Container The erase operation is often used after finding an element that should be removed from the container. The easiest way to find a given element is to use the library find algorithm find returns an iterator referring to the first element with that value or the off-the-end iterator string searchvalue("quasimodo"); list<string>::iterator iter = find(slist.begin(), slist.end(), searchvalue); 83 if (iter!= slist.end()) slist.erase(iter); 84 21

22 Removing All the Elements in a Container Removing All the Elements in a Container To delete all the elements in a container, we could either call clear or pass the iterators from begin and end to erase slist.clear(); // delete all the elements within the container slist.erase(slist.begin(), slist.end()); // equivalent The iterator-pair version of erase lets us delete a subrange of elements // delete range of elements between two values list<string>::iterator elem1, elem2; // elem1 refers to val1 elem1 = find(slist.begin(), slist.end(), val1); // elem2 refers to the first occurrence of val2 after val1 elem2 = find(elem1, slist.end(), val2); 85 // erase range from val1 up to but not including val2 slist.erase(elem1, elem2); 86 Assignment and swap The assignment-related operators act on the entire container Except for swap, they can be expressed in terms of erase and insert operations The assignment operator erases the entire range of elements in the left-hand container and then inserts the elements of the right-hand container object into the left-hand container Assignment and swap c1 = c2; // replace contents of c1 with a copy of elements in c2 // equivalent operation using erase and insert c1.erase(c1.begin(), c1.end()); // delete all elements in c1 c1.insert(c1.begin(), c2.begin(), c2.end()); // insert c2 After the assignment, the left- and right-hand containers are equal: Even if the containers had been of unequal size, after the assignment both containers have the size of the right-hand operand Assignment and the assign operations invalidate all iterators into the left-hand container

23 Assignment and swap The assign operation deletes all the elements in the container and then inserts new elements as specified by the arguments Like the constructor that copies elements from a container, the assignment operator (=) can be used to assign one container to another only if the container and element type are the same If we want to assign elements of a different but compatible element type and/or from a different container type, then we must use the assign operation. For example, we could use assign to assign a range of char* values from a vector into a list of string. Assignment and swap c.assign(b,e) Replaces the elements in c by those in the range denoted by iterators b and e. The iterators b and e must not refer to elements in c. c.assign(n,t) Replaces the elements in c by n elements with value t Assignment and swap The arguments to assign determine how many elements are inserted and what values the new elements will have // equivalent to slist1 = slist2 slist1.assign(slist2.begin(), slist2.end()); A second version of assign takes an integral value and an element value. It replaces the elements in the container by the specified number of elements, each of which has the specified element value Assignment and swap c1.swap(c2) Swaps contents: After the call c1 has elements that were in c2, and c2 has elements that were in c1. c1 and c2 must be the same type. Execution time usually much faster than copying elements from c2 to c1. // equivalent to: slist1.clear(); // followed by slist1.insert(slist1.begin(), 10, "Hiya!"); slist1.assign(10, "Hiya!"); // 10 elements; each one is Hiya!

24 Using swap to Avoid the Cost of Deleting Elements The swap operation swaps the values of its two operands. The types of the containers must match: The operands must be the same kind of container, and they must hold values of the same type After the call to swap, the elements that had been in the right-hand operand are in the left, and vice versa vector<string> svec1(10); // vector with 10 elements vector<string> svec2(24); // vector with 24 elements svec1.swap(svec2); Using swap to Avoid the Cost of Deleting Elements swap does not invalidate iterators. After swap, iterators continue to refer to the same elements, although those elements are now in a different container For example, had iter referred to the string at position svec1[3] before the swap it will refer to the element at position svec2[3] after the swap. vector<string> svec1(10); // vector with 10 elements vector<string> svec2(24); // vector with 24 elements svec1.swap(svec2); How a vector Grows When we insert or push an element onto a container object, the size of that object increases by one Similarly, if we resize a container to be larger than its current size, then additional elements must be added to the container The library takes care of allocating the memory to hold these new elements 95 How a vector Grows To support fast random access, vector elements are stored contiguously: each element is adjacent to the previous element If there is no room in the vector for the new element, it cannot just add an element somewhere else in memory because the elements must be contiguous for indexing to work Instead, the vector must allocate new memory to hold the existing elements plus the new one, copy the elements from the old location into the new space, add the new element, and deallocate the old memory If vector did this memory allocation and deallocation each time we added an element, then performance would be unacceptably slow

25 How a vector Grows How a vector Grows There is no comparable allocation issue for containers that do not hold their elements contiguously For example, to add an element to a list, the library only needs to create the new element and chain it into the existing list. There is no need to reallocate or copy any of the existing elements. For most applications the best container to use is a vector. The reason is that library uses allocation strategies that minimize the costs of storing elements contiguously That cost is usually offset by the advantages in accessing elements that contiguous storage allows. 97 The way vectors achieve fast allocation is by allocating capacity beyond what is immediately needed Thus, there is no need to reallocate the container for each new element The exact amount of additional capacity allocated varies across different implementations of the library. This allocation strategy is dramatically more efficient than reallocating the container each time an element is added In fact, its performance is good enough that in practice a vector usually grows more efficiently than a list or a deque. 98 capacity and reserve Members capacity and reserve Members The details of how vector handles its memory allocation are part of its implementation However, a portion of this implementation is supported by the interface to vector The vector class includes two members, capacity and reserve, that let us interact with the memoryallocation part of vector's implementation The capacity operation tells us how many elements the container could hold before it must allocate more space The reserve operation lets us tell the vector how many elements it should be prepared to hold. 99 vector<int> ivec; // size should be zero; capacity is implementation defined cout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl; // give ivec 24 elements for (vector<int>::size_type ix = 0; ix!= 24; ++ix) ivec.push_back(ix); // size should be 24; capacity will be >= 24 and is // implementation defined cout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl; ivec: size: 0 capacity: 0 ivec: size: 24 capacity:

26 capacity and reserve Members capacity and reserve Members We could now reserve some additional space: ivec.reserve(50); // sets capacity to at least 50; might be more // size should be 24; capacity will be >= 50 and is implementation defined cout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl; We might next use up that reserved capacity as follows: // add elements to use up the excess capacity while (ivec.size()!= ivec.capacity()) ivec.push_back(0); // size should be 50; capacity should be unchanged cout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl; ivec: size: 24 capacity: As long as there is excess capacity, the vector must not reallocate its elements. The output indicates that at this point we've used up the reserved capacity, and size and capacity are equal: ivec: size: 50 capacity: capacity and reserve Members capacity and reserve Members If we now add another element, the vector will have to reallocate itself: ivec.push_back(42); // add one more element // size should be 51; capacity will be >= 51 and is // implementation defined cout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl; The output from this portion of the program ivec: size: 51 capacity: 100 Indicates that this vector implementation appears to follow a strategy of doubling the current capacity each time it has to allocate new storage 103 Each implementation of vector is free to choose its own allocation strategy However, it must provide the reserve and capacity functions, and it must not allocate new memory until it is forced to do so How much memory it allocates is up to the implementation Different libraries will implement different strategies

27 Deciding Which Container to Use As we saw in the previous section, allocating memory to hold elements in contiguous storage has impacts on the memory allocation strategies and overhead of a container By using clever implementation techniques, library authors minimize this allocation overhead Whether elements are stored contiguously has other significant impacts on: The costs to add or delete elements from the middle of the container The costs to perform nonsequential access to elements of the container Deciding Which Container to Use The degree to which a program does these operations should be used to determine which type of container to use The vector and deque types provide fast non-sequential access to elements at the cost of making it expensive to add or remove elements anywhere other than the ends of the container The list type supports fast insertion and deletion anywhere but at the cost of making nonsequential access to elements expensive How Insertion Affects Choice of Container A list represents noncontiguous memory and allows for both forward and backward traversal one element at a time It is efficient to insert or erase an element at any point Inserting or removing an element in a list does not move any other elements. Random access, on the other hand, is not supported Accessing an element requires traversing the intervening elements. How Insertion Affects Choice of Container Inserting (or removing) anywhere except at the back of a vector requires that each element to the right of the inserted (or deleted) element be moved For example, if we have a vector with 50 elements and we wish to erase element number 23, then each of the elements after 23 have to be moved forward by one position Otherwise, there'd be a hole in the vector, and the vector elements would no longer be contiguous

28 How Insertion Affects Choice of Container A deque is a more complicated data structure. We are guaranteed that adding or removing elements from either end of the deque is a fast operation Adding or removing from the middle will be more expensive A deque offers some properties of both list and vector: Like vector, it is inefficient to insert or erase elements in the middle of the deque. Unlike vector, a deque offers efficient insert and erase at the front as well as at the back. Unlike list and like vector, a deque supports fast random access to any element. How Insertion Affects Choice of Container Inserting elements at the front or back of a deque does not invalidate any iterators Erasing the front or back element invalidates only iterators referring to the element(s) erased Inserting or erasing anywhere else in the deque invalidates all iterators referring to elements of the deque How Access to the Elements Affects Choice of Container Both vector and deque support efficient random access to their elements That is, we can efficiently access element 5, then 15, then 7, and so on. Random access in a vector can be efficient because each access is to a fixed offset from the beginning of the vector. It is much slower to jump around in a list. the only way to move between the elements of a list is to sequentially follow the pointers Moving from the 5th to the 15th element requires visiting every element between them 111 Hints on Selecting Which Container to Use There are a few rules of thumb that apply to selecting which container to use: If the program requires random access to elements, use a vector or a deque. If the program needs to insert or delete elements in the middle of the container, use a list. If the program needs to insert or delete elements at the front and the back, but not in the middle, of the container, use a deque

29 Hints on Selecting Which Container to Use If we need to insert elements in the middle of the container only while reading input and then need random access to the elements, consider reading them into a list and then reordering the list as appropriate for subsequent access and copying the reordered list into a vector. Hints on Selecting Which Container to Use What if the program needs to randomly access and insert and delete elements in the middle of the container? This decision will depend on the relative cost of doing random access to list elements versus the cost of copying elements when inserting or deleting elements in a vector or deque In general, the predominant operation of the application (whether it does more access or more insertion or deletion) should determine the choice of container type strings Revisited strings Revisited We can create a string: as the empty string, by providing no argument; as a copy of another string; from a pair of iterators; or from a count and a character string s1; // s1 is the empty string string s2(5, 'a'); // s2 == "aaaaa" string s3(s2); // s3 is a copy of s2 string s4(s3.begin(), s3.begin() + s3.size() / 2); // s4 == "aa" 115 There is another constructor that takes a pointer to an element in a character array and a count of how many characters to copy. Because the constructor takes a count, the array does not have to be nullterminated char *cp = "Hiya"; // null-terminated array char c_array[] = "World!!!!"; // null-terminated char no_null[] = {'H', 'i'}; // not null-terminated string s1(cp); // s1 == "Hiya" string s2(c_array, 5); // s2 == "World" string s3(c_array + 5, 4); // s3 == "!!!!" string s4(no_null); // runtime error: not null-terminated string s5(no_null, 2); // ok: s5 == "Hi"

30 Other Ways to Change a string Many of the container operations that string supports operate in terms of iterators For example, erase takes an iterator or iterator range to specify which element(s) to remove from the container Similarly, the first argument to each version of insert takes an iterator to indicate the position before which to insert the values represented by the other arguments. Other Ways to Change a string s.insert(p, t) Insert copy of value t before element referred to by iterator p s.insert(p, n, t) Insert n copies of t before p. Returns void s.insert(p, b, e) Insert elements in range denoted by iterators b and e before p. Returns void Other Ways to Change a string s.assign(b, e) Replace s by elements in range denoted by b and e. For string, returns s, for the containers, returns void. s.assign(n, t) Replace s by n copies of value t. For string, returns s, for the containers, returns void. s.erase(p) Erase element referred to by iteartor p.returns an iterator to the element after the one deleted s.erase(b, e) Remove elements in range denoted by b and e. Returns an iterator to the first element after the range deleted. 119 Specifying the New Contents The characters to insert or assign into the string can be taken from a character array or another string For example, we can use a null-terminated character array as the value to insert or assign into a string: char *cp = "Stately plump Buck"; string s; s.assign(cp, 7); // s == "Stately" s.insert(s.size(), cp + 7); // s == "Stately plump Buck"

31 Specifying the New Contents Similarly, we can insert a copy of one string into another as follows s = "some string"; s2 = "some other string"; // 3 equivalent ways to insert all the characters from s2 // insert iterator range before s.begin() s.insert(s.begin(), s2.begin(), s2.end()); // insert copy of s2 before position 0 in s s.insert(0, s2); // insert s2.size() characters from s2 starting at s2[0] s.insert(0, s2, 0, s2.size()); string-only Operations The string type provides several other operations that the containers do not: The substr function that returns a substring of the current string The append and replace functions that modify the string A family of find functions that search the string The substr Operation The substr operation lets us retrieve a substring from a given string We can pass substr a starting position and a count. It creates a new string that has that many characters, (up to the end of the string) from the target string, starting at the given position string s("hello world"); // return substring of 5 characters starting at position 6 string s2 = s.substr(6, 5); // s2 = world Alternatively, we could obtain the same result by writing: // return substring from position 6 to the end of s string s3 = s.substr(6); // s3 = world 123 The append and replace Functions There are six overloaded versions of append and ten versions of replace. The append and replace functions are overloaded using the same set of arguments These arguments specify the characters to add to the string. In the case of append, the characters are added at the end of the string In the replace function, these characters are inserted in place a specified range of existing characters in the string

32 The append and replace Functions The append operation is a shorthand way of inserting at the end string s("c++ Primer"); // initialize s to "C++ Primer" s.append(" 3rd Ed."); // s == "C++ Primer 3rd Ed." // equivalent to s.append(" 3rd Ed.") s.insert(s.size(), " 3rd Ed."); The append and replace Functions The replace operations remove an indicated range of characters and insert a new set of characters in their place. The replace operations have the same effect as calling erase and insert // starting at position 11, erase 3 characters and then insert "4th" s.replace(11, 3, "4th"); // s == "C++ Primer 4th Ed." // equivalent way to replace "3rd" by "4th" s.erase(11, 3); // s == "C++ Primer Ed." s.insert(11, "4th"); // s == "C++ Primer 4th Ed." The append and replace Functions In the previous call to replace, the text we inserted happens to be the same size as the text we removed. We could insert a larger or smaller string s.replace(11, 3, "Fourth"); // s == "C++ Primer Fourth Ed." 127 string Search Operations The string class provides six search functions, each named as a variant of find The operations all return a string::size_type value that is the index of where the match occurred, or a special value named string::npos if there is no match There are four versions of each of the search operations, each of which takes a different set of arguments Basically, these operations differ as to whether they are looking for a single character, another string, a C- style, null-terminated string, or a given number of characters from a character array

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators,

More information

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately, erasing items

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works Type Aliases A name may be defined as a synonym for an existing type name. Traditionally, typedef is used for this purpose. In the new standard, an alias declaration can also be used C++11.Thetwoformsareequivalent.

More information

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Review from Lecture 7 CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

More information

Chapter 13: Copy Control. Overview. Overview. Overview

Chapter 13: Copy Control. Overview. Overview. Overview Chapter 13: Copy Control Overview The Copy Constructor The Assignment Operator The Destructor A Message-Handling Example Managing Pointer Members Each type, whether a built-in or class type, defines the

More information

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators Review from Lecture 7 Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

More information

Abstract Container Types

Abstract Container Types 6 Abstract Container Types This chapter provides both an extension of and completion to Chapters 3 and 4. We continue the discussion of types started in Chapter 3 by presenting more information on the

More information

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Today CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Another vector operation: pop back Erasing items from vectors is inefficient! Iterators and iterator operations

More information

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11 Di erent Kinds of Containers Container Notes A container is an object that stores other objects and has methods for accessing the elements. There are two fundamentally di erent kinds of containers: Sequences

More information

Programming with Haiku

Programming with Haiku Programming with Haiku Lesson 2 Written by DarkWyrm All material 2010 DarkWyrm In our first lesson, we learned about how to generalize type handling using templates and some of the incredibly flexible

More information

Containers in C++ and Java

Containers in C++ and Java Containers in C++ and Java 1 1. Which of the following statements is true? a) Equality on the basis of reference implies equality on the basis of content. b) Equality on the basis of content implies equality

More information

Arrays and Pointers. Overview. Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays

Arrays and Pointers. Overview. Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays Arrays and Pointers Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays 1 Overview C++ defines two lower-level compound types: arrays and pointers that are similar to vectors

More information

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4 Steven J. Zeil July 19, 2013 Contents 1 Overview of Sets and Maps 4 1 2 The Set ADT 6 2.1 The template header................................. 14 2.2 Internal type names.................................

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

MODULE 33 --THE STL-- ALGORITHM PART I

MODULE 33 --THE STL-- ALGORITHM PART I MODULE 33 --THE STL-- ALGORITHM PART I My Training Period: hours Note: Compiled using Microsoft Visual C++.Net, win32 empty console mode application. g++ compilation examples given at the end of this Module.

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Chapter 5. The Standard Template Library.

Chapter 5. The Standard Template Library. Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library. 5.1. Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov,

More information

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed

More information

Exercise 6.2 A generic container class

Exercise 6.2 A generic container class Exercise 6.2 A generic container class The goal of this exercise is to write a class Array that mimics the behavior of a C++ array, but provides more intelligent memory management a) Start with the input

More information

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second C++ Vector Constructors explicit vector ( const Allocator& = Allocator() ); explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); template vector (

More information

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

C++ 11 and the Standard Library: Containers, Iterators, Algorithms and the Standard Library:,, Comp Sci 1575 Outline 1 2 3 4 Outline 1 2 3 4 #i n clude i n t main ( ) { i n t v a l u e 0 = 5 ; // C++ 98 i n t v a l u e 1 ( 5 ) ; // C++ 98 i n t v a

More information

Heap Management. Heap Allocation

Heap Management. Heap Allocation Heap Management Heap Allocation A very flexible storage allocation mechanism is heap allocation. Any number of data objects can be allocated and freed in a memory pool, called a heap. Heap allocation is

More information

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure

More information

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators Session 5 - Pointers and Arrays Iterators Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Introduction Pointers and arrays: vestiges

More information

C++ Programming Chapter 7 Pointers

C++ Programming Chapter 7 Pointers C++ Programming Chapter 7 Pointers Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department

More information

Exceptions, Templates, and the STL

Exceptions, Templates, and the STL Exceptions, Templates, and the STL CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 16 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/

More information

Concurrent Data Structures in C++ CSInParallel Project

Concurrent Data Structures in C++ CSInParallel Project Concurrent Data Structures in C++ CSInParallel Project July 26, 2012 CONTENTS 1 Concurrent Data Structures in C++: Web crawler lab 1 1.1 Your goals................................................ 1 1.2

More information

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors 590 18. Dynamic Data Structures I Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors Recap: vector 591 Can be initialised with arbitrary size n 591 Recap: vector

More information

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

Vector. Vector Class. class Vector { public: typedef unsigned int size_type; Vector Arrays in C++ must be pre-allocated to accomodate the maximum number of elements. Should an application attempt to add more than this number, the program may abort, or alter storage of adjacent

More information

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness. Iterators Overview We have introduced, used, built, and studied iterators in several contexts, including List, TDeque, and TVector. We have seen that ordinary pointers also can be thought of as iterators

More information

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information

List, Stack, and Queues

List, Stack, and Queues List, Stack, and Queues R. J. Renka Department of Computer Science & Engineering University of North Texas 02/24/2010 3.1 Abstract Data Type An Abstract Data Type (ADT) is a set of objects with a set of

More information

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation Outline EDAF30 Programming in C++ 4. The standard library. Algorithms and containers. Sven Gestegård Robertz Computer Science, LTH 2018 1 Type inference 2 3 The standard library Algorithms Containers Sequences

More information

Standard Library Reference

Standard Library Reference Standard Library Reference This reference shows the most useful classes and functions in the standard library. Note that the syntax [start, end) refers to a half-open iterator range from start to end,

More information

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2 TEMPLATES Today This lecture looks at techniques for generic programming: Generic pointers Templates The standard template library Thebestreferenceis: http://www.cppreference.com/index.html andyoucanalsoconsultchapters6amd7inthetextbook.

More information

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Test 1 102 total points on the test. Therefore, your score is out of 102. Class average: 89.6 / 102 Distribution: Solutions are posted

More information

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming What the heck is STL???? Another hard to understand and lazy to implement stuff? Standard Template Library The standard template

More information

Advanced Programming in C++ Container Design I

Advanced Programming in C++ Container Design I Advanced Programming in C++ Container Design I This is an exercise on container design, especially in the view of robustness, iterators, and storage management. The container will be developed step-by-step,

More information

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.) Lecture 10 Class string Namespaces Preprocessor directives Macros Conditional compilation Command line arguments Character handling library void* TNCG18(C++): Lec 10 1 Class string Template class

More information

CS11 Advanced C++ Spring 2018 Lecture 2

CS11 Advanced C++ Spring 2018 Lecture 2 CS11 Advanced C++ Spring 2018 Lecture 2 Lab 2: Completing the Vector Last week, got the basic functionality of our Vector template working It is still missing some critical functionality Iterators are

More information

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures Lecture 10 Class string Exception Handling To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures Class template auto_ptr Lec 10 Programming in C++ 1 Class

More information

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions 1. You are given a map that associates strings with lists of strings. The definition is: map words; Write

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

use static size for this buffer

use static size for this buffer Software Design (C++) 4. Templates and standard library (STL) Juha Vihavainen University of Helsinki Overview Introduction to templates (generics) std::vector again templates: specialization by code generation

More information

Patterns: Working with Arrays

Patterns: Working with Arrays Steven Zeil October 14, 2013 Outline 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates

More information

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland May 6, 2010 ENGI

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Alice E. Fischer Lecture 3 217 Alice E. Fischer Data Structures L3... 1/31 1 Arrays Growing Arrays vectors 2 3 Empty Lists Inserting Data - Unsorted Inserting Data - Sorted

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

Advanced C++ STL. Tony Wong

Advanced C++ STL. Tony Wong Tony Wong 2017-03-25 C++ Standard Template Library Algorithms Sorting Searching... Data structures Dynamically-sized array Queues... The implementation is abstracted away from the users The implementation

More information

PIC 10A. Lecture 23: Intro to STL containers

PIC 10A. Lecture 23: Intro to STL containers PIC 10A Lecture 23: Intro to STL containers STL STL stands for Standard Template Library There are many data structures that share similar features These data structures can be manipulated by a common

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 An important part of every container is the type of iterator it supports. This determines which algorithms can be applied to the container. A vector supports random-access iterators i.e., all iterator

More information

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection Deallocation Mechanisms User-controlled Deallocation Allocating heap space is fairly easy. But how do we deallocate heap memory no longer in use? Sometimes we may never need to deallocate! If heaps objects

More information

15. Pointers, Algorithms, Iterators and Containers II

15. Pointers, Algorithms, Iterators and Containers II 498 Recall: Pointers running over the Array 499 Beispiel 15. Pointers, Algorithms, Iterators and Containers II int a[5] = 3, 4, 6, 1, 2; for (int p = a; p < a+5; ++p) std::cout

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

CS11 Advanced C++ Fall Lecture 1

CS11 Advanced C++ Fall Lecture 1 CS11 Advanced C++ Fall 2006-2007 Lecture 1 Welcome! ~8 lectures Slides are posted on CS11 website http://www.cs.caltech.edu/courses/cs11 ~6 lab assignments More involved labs 2-3 week project at end CS

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin Permission is granted to copy and distribute freely, for any purpose, provided the copyright notice above is

More information

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

More information

Template based set of collection classes STL collection types (container types)

Template based set of collection classes STL collection types (container types) STL Collection Types Template based set of collection classes STL collection types (container types) Sequences vector - collection of elements of type T list - doubly linked list, only sequential access

More information

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8 today: strings what are strings what are strings and why to use them reading: textbook chapter 8 a string in C++ is one of a special kind of data type called a class we will talk more about classes in

More information

Think Like a Programmer

Think Like a Programmer Think Like a Programmer Errata, Clarifications, and Updates Last Updated 2/21/2017 This lists errata and clarifications going back to the first printing; almost all of these issues are already corrected

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

STL Quick Reference for CS 241

STL Quick Reference for CS 241 STL Quick Reference for CS 241 Spring 2018 The purpose of this document is to provide basic information on those elements of the C++14 standard library we think are most likely to be needed in CS 241.

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

14. Pointers, Algorithms, Iterators and Containers II

14. Pointers, Algorithms, Iterators and Containers II Recall: Pointers running over the Array Beispiel 14. Pointers, Algorithms, Iterators and Containers II Iterations with Pointers, Arrays: Indices vs. Pointers, Arrays and Functions, Pointers and const,

More information

1. The term STL stands for?

1. The term STL stands for? 1. The term STL stands for? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d 2. Which of the following statements regarding the

More information

Function Templates. Consider the following function:

Function Templates. Consider the following function: Function Templates Consider the following function: void swap (int& a, int& b) { int tmp = a; a = b; b = tmp; Swapping integers. This function let's you swap the contents of two integer variables. But

More information

Lesson 13 - Vectors Dynamic Data Storage

Lesson 13 - Vectors Dynamic Data Storage Lesson 13 - Vectors Dynamic Data Storage Summary In this lesson we introduce the Standard Template Library by demonstrating the use of Vectors to provide dynamic storage of data elements. New Concepts

More information

Programming in C++ using STL. Rex Jaeschke

Programming in C++ using STL. Rex Jaeschke Programming in C++ using STL Rex Jaeschke Programming in C++ using STL 1997, 1999, 2002, 2007, 2009 Rex Jaeschke. All rights reserved. Edition: 2.0 All rights reserved. No part of this publication may

More information

SSE2034: System Software Experiment 3

SSE2034: System Software Experiment 3 SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu STL Collection Types Template based set of collection

More information

7.1 Optional Parameters

7.1 Optional Parameters Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default

More information

Copy Control 2008/04/08. Programming Research Laboratory Seoul National University

Copy Control 2008/04/08. Programming Research Laboratory Seoul National University Copy Control 2008/04/08 Soonho Kong soon@ropas.snu.ac.kr Programming Research Laboratory Seoul National University 1 Most of text and examples are excerpted from C++ Primer 4 th e/d. 2 Types control what

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47 Lecture 8 Xiaoguang Wang STAT 598W February 13th, 2014 (STAT 598W) Lecture 8 1 / 47 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 2 / 47 Outline 1 Introduction: C++ 2 Containers

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 Data that is formatted and written to a sequential file as shown in Section 17.4 cannot be modified without the risk of destroying other data in the file. For example, if the name White needs to be changed

More information

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types Arrays - Vectors Data Types Data Type: I. set of values II. set of operations over those values Example: Integer I. whole numbers, -32768 to 32767 II. +, -, *, /, %, ==,!=, , =,... Which operation

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types. Session 4 - Genericity, Containers Polymorphism Code that works for many types. Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson)

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Dodatak D Standardna string klasa

Dodatak D Standardna string klasa Dodatak D Standardna klasa from: C++ annotations, by Frank C. Brokken, University of Groningen, ISBN 90 367 0470. The C programming language offers rudimentary support: the ASCII-Z terminated series of

More information

Sequential Containers. Ali Malik

Sequential Containers. Ali Malik Sequential Containers Ali Malik malikali@stanford.edu Game Plan Recap Overview of STL Sequence Containers std::vector std::deque Container Adapters Announcements Recap getline vs >> Bug favnum = fullname

More information

Arrays and Linked Lists

Arrays and Linked Lists Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every

More information

CS197c: Programming in C++

CS197c: Programming in C++ CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html Administration HW1 will be up this afternoon Written assignment Due in class next week See website

More information

Data Structures Lecture 3 Order Notation and Recursion

Data Structures Lecture 3 Order Notation and Recursion Data Structures Lecture 3 Order Notation and Recursion 1 Overview The median grade.cpp program from Lecture 2 and background on constructing and using vectors. Algorithm analysis; order notation Recursion

More information

C++ Programming Lecture 2 Software Engineering Group

C++ Programming Lecture 2 Software Engineering Group C++ Programming Lecture 2 Software Engineering Group Philipp D. Schubert Contents 1. Compiler errors 2. Functions 3. std::string 4. std::vector 5. Containers 6. Pointer and reference types Compiler errors

More information

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently. The Science of Computing I Lesson 4: Introduction to Data Structures Living with Cyber Pillar: Data Structures The need for data structures The algorithms we design to solve problems rarely do so without

More information

Writing Generic Functions. Lecture 20 Hartmut Kaiser hkaiser/fall_2013/csc1254.html

Writing Generic Functions. Lecture 20 Hartmut Kaiser  hkaiser/fall_2013/csc1254.html Writing Generic Functions Lecture 20 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2013/csc1254.html Code Quality Programming Principle of the Day Minimize Coupling - Any section

More information

12. Pointers Address-of operator (&)

12. Pointers Address-of operator (&) 12. Pointers In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifer (their name). This way, the program does not need to care

More information

Chapter Six: Arrays and Vectors

Chapter Six: Arrays and Vectors Chapter Six: Arrays and Vectors Slides by Evan Gallagher Chapter Goals To become familiar with using arrays and vectors to collect values To learn about common algorithms for processing arrays and vectors

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 103 Unit 11. Linked Lists. Mark Redekopp 1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++

More information

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible

More information

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection

More information