Collections
Data structures Data Structures ( โครงสร้างข้อมูล ) เกิดจากการ นำข้อมูลขั้นพื้นฐานที่แบ่งแยกไม่ได้ (atomic data type) เช่น int, char, double และ enum มา ประกอบเข้าด้วยกันกลายเป็นข้อมูลที่มี โครงสร้างใหญ่ขึ้น ซับซ้อนมากขึ้น วิธีการนำ ข้อมูลมาประกอบเข้าด้วยกัน ก่อให้เกิด โครงสร้างข้อมูลในรูปแบบต่างๆ เช่น List, Linked List, Stack, Queue, Tree, Graph, etc.
Abstract data type (ADT) Abstract data type (ADT) คือ ชนิดข้อมูลที่มีการ นิยามถึงพฤติกรรม (behavior) หรือวิธีการ นำไปใช้ มากกว่ารูปแบบข้อมูลที่เก็บอยู่ (representation) ADT นี้เป็นหัวใจสำคัญของวิธีการเขียนโปรแกรม แบบวัตถุ นั่นคือ เมื่อเราสร้างคลาสวัตถุ เรา จะต้องนึกถึง method ที่จะใช้ได้กับวัตถุในคลาส นั้นๆ
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, toString) – Representation ( int, int) Student – Behavior (getID, getName, getCredits, setID, setName, setCredits, toString) – 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 vectorFlexible-size array dequeDouble-ended queue. listDoubly linked list. forward_listSingly linked list. arrayFixed-size array. stringA specialized container, similar to vector, that contains characters
Container Adaptors stackBy default stack is implemented in terms of deque. queueBy default queue is implemented in terms of deque priority_queuepriority_queue is implemented on a vector.
Associative Containers Elements Ordered by Key mapAssociative array; holds key-value pairs setContainer in which the key is the value multimapMap in which a key can appear multiple times multisetSet in which a key can appear multiple times
Associative Containers Unordered collections unordered_mapMap organized by hash function unordered_setSet organized by hash function unordered_multimapHashed map; keys can appear multiple times unordered_multisetHashed set; keys can appear multiple times
Vector (ArrayList in Java) #include using std::vector; vector ivec; vector student; vector > file;// C++ 11 vector > file; // Old style
Defining and Initializing vectors vector svec;// svec is empty vector ivec;// ivec is empty vector ivec2(ivec);// copy ivec to ivec2 vector ivec3 = ivec;// copy ivec to ivec3 vector svec(ivec2)// error: svec holds // string
List Initializing a vector vector articles = {“a”, “an”, “the”}; vector v1{“a”, “an”, “the”}; vector v1(“a”, “an”, “the”);//error
Creating a specified number of Elements // ten int elements, each initialized to -1 vector ivec(10, -1); // ten strings, each element is “hi!” vector svec(10, “hi!”); // ten int elements, each initialized to 0 vector ivec(10); // ten strings, each an empty string vector svec(10); vector vi = 10;// Error: must use direct initialization
List Initializer or Element Count? vector v1(10); // v1 has ten elements with value 0 vector v2{10}; // v2 has one element with value 0 vector v3(10, 1); // v3 has ten elements with value 1 vector v4{10, 1}; // v4 has two elements with value 10 and 1
List Initializer or Element Count? vector v5{“hi”}; // v5 has one element vector v6(“hi”); // error vector v7{10}; // v7 has ten elements vector v8{10, “hi”}; // v8 has ten elements with // value hi
Adding elements to a vector vector v2; for (int i = 0; i != 100; ++i) v2.push_back(i); string word; vector text; while (cin >> word) { text.push_back(word); }
Other vector operation vector 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)// for each element in v cout << i << “ “;// print the element cout << endl;
Vector Operations v.empty() // Return true if v is empty v.size() // Return the number of element v.push_back(t) // Add an element with value t v[n] // Return a reference to the element // at position n in v v1 = v2// v1 = {a, b, c…} v1 == v2 v1 != v2, >=
vector scores(11, 0); unsigned grade; while (cin >> grade) { if (grade <= 100) ++scores[grade/10]; } Input: Output:
vector 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 ivec; // empty vector cout << ivec[0]; // error: ivec has no element vector ivec2(10);// vector with ten element cout << ivec2[10]; // error: ivec2 has element 0..9