100 likes | 215 Views
Lecture 10. STL & Data Structure & Algorithm. vector. vector<string> SS; SS.push_back("The number is 10"); SS.push_back("The number is 20"); for(int ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; }. vector. Continous Storage. What about deleting one element in the middle?.
E N D
Lecture 10 STL & Data Structure & Algorithm
vector vector<string> SS; SS.push_back("The number is 10"); SS.push_back("The number is 20"); for(int ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; }
vector • Continous Storage. • What about deleting one element in the middle? CSE459.22 Programming in C++ Au09, The Ohio State University
List list<int> L; L.push_back(0); L.push_front(0); list<int>::iterator i; for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; CSE459.22 Programming in C++ Au09, The Ohio State University
List • Non-continous Storage • Randomly delete one element efficiently. CSE459.22 Programming in C++ Au09, The Ohio State University
Map map<int, string> Employees; Employees[5234] = "Mike C."; Employees[3374] = "Charlie M."; Employees[1923] = "David D."; cout << "Employees[3374]=" << Employees[3374] << endl << endl; CSE459.22 Programming in C++ Au09, The Ohio State University
Queue queue<int> Q; Q.push(8); Q.push(7); Q.push(6); Q.pop(); //pop 8 Q.pop(); // pop 7 Q.pop(); // pop 6 CSE459.22 Programming in C++ Au09, The Ohio State University
priority_queue priority_queue<int> mypq; mypq.push(30); mypq.push(100); mypq.push(25); while (!mypq.empty()) { cout << " " << mypq.top(); mypq.pop(); } //Output: 100 30 25 CSE459.22 Programming in C++ Au09, The Ohio State University
priority_queue priority_queue <int, vector<int>, greater<int> > pq; /*pq is a priority queue of integers that uses a vector of integers for storage and uses > to determine priority. This means that if a > b, a has *lower* priority than b.*/ • pq.push(2); //put 2, 5, 3, 1 into the queue • pq.push(5); • pq.push(3); • pq.push(1); • while (!pq.empty()) { • cout << pq.top() << endl; • pq.pop(); //output 1 2 3 5 • } CSE459.22 Programming in C++ Au09, The Ohio State University
priority_queue • See Height.pdf CSE459.22 Programming in C++ Au09, The Ohio State University