Collections Behavior and representation Integer Behavior etc Representation
Collections
Behavior and representation • Integer – Behavior (+, -, *, /, %, <, <=, >, >=, etc) – Representation (1’s complement, 2’s complement, sign magnitude, etc) • Double – Behavior (+, -, *, /, <, <=, >, >=, etc) – Representation (pure notation, excess or biased notation, etc)
Behavior and representation • Alphanumeric (character) – Behavior (display, >, >=, <, <=, etc) – Representation (Unicode, ASCII, EBCDIC, etc) • String – Behavior (concat, extract, >, >=, <, <=, etc) – Representation (array of character, etc)
Behavior and representation • Rational – Behavior (add, subtract, multiply, divide, to. String) – Representation ( int, int) • Student – Behavior (get. ID, get. Name, get. Credits, set. ID, set. Name, set. Credits, to. String) – Representation (int, string, double)
Collections • Vector , Stack, Queue, Map, Set, etc • Each of these classes contains a collection of values of some simpler type. • Learning how to use • Learning how to implement efficiently (underlying algorithms and data structures)
Sequential Containers vector deque list forward_list array string Flexible-size array Double-ended queue. Doubly linked list. Singly linked list. Fixed-size array. A specialized container, similar to vector, that contains characters
Container Adaptors stack queue priority_queue By default stack is implemented in terms of deque. By default queue is implemented in terms of deque priority_queue is implemented on a vector.
Associative Containers Elements Ordered by Key map Associative array; holds key-value pairs set Container in which the key is the value multimap Map in which a key can appear multiple times multiset Set in which a key can appear multiple times
Associative Containers Unordered collections unordered_map unordered_set unordered_multimap unordered_multiset Map organized by hash function Set organized by hash function Hashed map; keys can appear multiple times Hashed set; keys can appear multiple times
Vector (Array. List in Java) #include <vector> using std: : vector; vector<int> ivec; vector<Student> student; vector<string>> file; vector<string> > file; // C++ 11 // Old style
Defining and Initializing vectors vector<string> svec; // svec is empty vector<int> ivec; // ivec is empty vector<int> ivec 2(ivec); // copy ivec to ivec 2 vector<int> ivec 3 = ivec; // copy ivec to ivec 3 vector<string> svec(ivec 2) // error: svec holds // string
List Initializing a vector<string> articles = {“a”, “an”, “the”}; vector<string> v 1(“a”, “an”, “the”); //error
Creating a specified number of Elements // ten int elements, each initialized to -1 vector<int> ivec(10, -1); // ten strings, each element is “hi!” vector<string> svec(10, “hi!”); // ten int elements, each initialized to 0 vector<int> ivec(10); // ten strings, each an empty string vector<string> svec(10); vector<int> vi = 10; // Error: must use direct initialization
List Initializer or Element Count? vector<int> v 1(10); vector<int> v 2{10}; // v 1 has ten elements with value 0 // v 2 has one element with value 0 vector<int> v 3(10, 1); // v 3 has ten elements with value 1 vector<int> v 4{10, 1}; // v 4 has two elements with value 10 and 1
List Initializer or Element Count? vector<string> v 5{“hi”}; vector<string> v 6(“hi”); vector<string> v 7{10}; vector<string> v 8{10, “hi”}; // v 5 has one element // error // v 7 has ten elements // v 8 has ten elements with // value hi
Adding elements to a vector<int> v 2; for (int i = 0; i != 100; ++i) v 2. push_back(i); string word; vector<string> text; while (cin >> word) { text. push_back(word); }
Other vector operation vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9}; for (auto &i : v) // for each element in v i *= i; // square the element value for (auto i : v) cout << i << “ “; cout << endl; // for each element in v // print the element
Vector Operations v. empty() v. size() v. push_back(t) v[n] v 1 = v 2 v 1 = {a, b, c…} v 1 == v 2 v 1 != v 2 <, <=, >, >= // Return true if v is empty // Return the number of element // Add an element with value t // Return a reference to the element // at position n in v //
vector<unsigned> scores(11, 0); unsigned grade; while (cin >> grade) { if (grade <= 100) ++scores[grade/10]; } Input: 42 65 95 100 39 67 95 76 88 76 83 92 76 93 Output: 0 0 0 1 1 0 2 3 2 4 1
vector<int> ivec; for (decltype (ivec. size()) ix = 0; ix != 10; ++ix) ivec[ix] = ix; // disaster: ivec has no elements for (decltype (ivec. size()) ix = 0; ix != 10; ++ix) ivec[ix]. push_back(ix); // ok adds a new element
vector<int> ivec; cout << ivec[0]; // empty vector // error: ivec has no element vector<int> ivec 2(10); // vector with ten element cout << ivec 2[10]; // error: ivec 2 has element 0. . 9
- Slides: 23