#include #include #include #include #include #include #include #include using namespace std; struct TestStr{ int weight,price; char c; }; class cmp{///used for the third argument in priority_queue. Has operator () with arguments two variables form the struct and compares them public: bool operator ()(const TestStr &a, const TestStr &b)const{ return a.weight > b.weight; } }; void vectorFunctions(){ vector container; container.push_back(3);///ads an element in the container, at the end container.push_back(1); container.push_back(2); printf("%d\n",container.size());///returns the number of elements in the vector container.pop_back();///removes the last element from the vector printf("%d\n",container.size()); printf("first element is %d\n",container[0]); container.push_back(1); container.push_back(2); for(int ind=0;ind reservedContainer; reservedContainer.reserve(10); } void queueFunctions(){ queue q; q.push(3);///ads an element in the queue q.push(2); q.push(7); q.push(9); printf("%d\n",q.front());///returns the first element in the queue q.pop();///removes the first element from the queue printf("%d\n",q.front()); printf("%d\n",q.empty());///returns true if the queue is empty, false otherwise while(!q.empty()){ printf("%d ",q.front()); q.pop(); } } void stackFunctions(){ stacks; s.push(3);///ads an element at the end of the stack s.push(1); s.push(7); printf("%d\n",s.top());///returns the last element in the stack s.pop();///removes the last element from the stack printf("%d\n",s.top()); while(!s.empty()){ printf("%d ",s.top()); s.pop(); } } void priority_queueFunctions(){ priority_queue h; ///heap. By default max heap h.push(3);///adds an element to the heap h.push(7); h.push(1); printf("%d\n",h.top());///returns the element at the top of the heap h.push(10); printf("%d\n",h.top()); h.pop();///removes the element at the top of the heap printf("%d\n",h.top()); while(!h.empty()){ printf("%d ",h.top()); h.pop(); } } void priority_queueFunctionsInversePriority(){ priority_queue,greater > minHeap;///first the data type, then container, then comparator class minHeap.push(3);///adds an element to the heap minHeap.push(7); minHeap.push(1); printf("%d\n",minHeap.top());///returns the element at the top of the heap } void priority_queueStruct(){ priority_queue, cmp> q;///using user defined comparator class q.push({1,2,'a'}); q.push({5,5,'b'}); q.push({3,3,'c'}); printf("%c\n",q.top().c); } void setFunctions(){ set s; s.insert(1);///adds an element to the set s.insert(6); s.insert(3); s.insert(3);///adding the same element a second time does nothing printf("%d\n",s.count(3));///returns the number of times an element is found in the set (0 or 1) printf("%d\n",s.count(5)); s.erase(3);///removes an element from the set printf("%d\n\n",s.count(3)); auto it = s.begin();///returns iterator to the first element; printf("%d\n",*it); it++; printf("%d\n",*it); s.insert(17); printf("%d\n",*(s.end()));///returns iterator to the first thing AFTER the last element. Printing the value is undefined behaviour for(auto it=s.begin(); it!=s.end(); it++){ printf("%d ",*it); } printf("\n"); auto it1 = s.find(6); it1++; printf("%d\n",*it1); it1 = s.find(7); printf("%d\n\n",it1 == s.end()); printf("%d\n",*s.lower_bound(6));///first equal or greater than printf("%d\n",*s.upper_bound(6));///first greater than printf("%d\n",*s.lower_bound(7)); printf("%d\n",*s.upper_bound(7)); printf("%d\n",s.upper_bound(18) == s.end()); } void mapFunctions(){ mapm;///contains pairs of key->value. A type of mapping m.insert({1,5.50});///insert the pair (1,5.50). (1 is the key, 5.50 is the value) m.insert({2,4.70}); m[3]=3.50; ///same as m.insert({3,3.50}); printf("%d\n%d\n",m.count(2),m.count(4));/// returns the number of times this key is found in the map printf("%f\n",m[3]);/// the value associated with this key m[2]=15.01;///changes the value associated with the key 2 printf("%f\n",m[2]); printf("%d\n",m.size()); } void pairFunctions(){ pair point,point2; ///ordered pair of values point.first=1;///first value point.second=6;///second value point2.first=1; point2.second=1; printf("%d\n",point.first); printf("%d\n",point v; int temp; while(scanf("%d",&temp) != EOF){ v.push_back(temp); } sort(v.begin(),v.end()); for(int i=0;i b.price; } void sortCustom(){ TestStr arr[5]; arr[0] = {1,2,'c'}; arr[1] = {3,4,'d'}; arr[2] = {5,6,'e'}; sort(arr,arr+3,cmpFunction);///the third argument is the above function. printf("%d %d %d\n",arr[0].price,arr[1].price,arr[2].price); } int main(){ sortCustom(); }