Untitled

 avatar
unknown
plain_text
2 years ago
6.3 kB
3
Indexable
*** linkedlist
#include<list>
#include<iostream>
#include <string>

using namespace std;
struct  Node
{
string name;
int old;

};

int pool;
Node POOL[50];
int main(){
list <int> mylist;
pool = 0;

POOL[pool].old = 1;
POOL[pool].name = "aaa";
mylist.push_front(pool); // insert dau vao list 
pool++;

POOL[pool].old = 2;
POOL[pool].name = "bbb";
mylist.push_front(pool);
pool++;

POOL[pool].old = 3;
POOL[pool].name = "ccc";
mylist.push_front(pool);
pool++;
cout << mylist.size() << endl;  //return size cua list
for(auto it = mylist.begin(); it != mylist.end(); it++){  // duyet list
	cout << POOL[*it].name << " " << POOL[*it].old << endl;
}
mylist.clear();  //reset list

mylist.empty(); // check list empty or not (true neu empty)
for(auto it = mylist.begin(); it != 	mylist.end(); ){
cout << POOL[*it].old << endl;
if(POOL[*it].old == 2){
	it = mylist.erase(it); // xoa 1 phan tu trong list
}else
{
	it++;
}
int it1 = mylist.front(); // tra ve gia tri dau list
int it2 = mylist.back(); // tra ve gia tri cuoi list


mylist.insert(mylist.end(), mylist2.begin(),mylist2.end()); // noi mylist2 vao mylist



{1,2,3,4,5}
it = mylist.begin(); -> 1
mylist.insert (it,10); //insert so 10 sau it -> {1,10,2,3,4,5} 

bool mycomparison (int first, int second)
{ 
return ( POOL[first].old < POOL[second].old ); 
}
mylist.sort(mycomparison); //sap xep theo ham mycomparison

mylist.pop_back();  // xoa cuoi list
mylist.pop_front() // xoa dau list
mylist.reverse(); //dao link list

int old_remove;
bool myremove(int first){
if(POOL[first].old == old_remove) return true;
return false;
} 

old_remove = 5;
mylist.remove_if(myremove); //xoa phan tu co old = 5


*** HASH
//Dùng để reset mảng về giá trị defaul
memset(POOL, null, sizeof(POOL))
Note: 
// include thư viện này trong bài thi
#include <bits/stdc++.h>

#include<unordered_map>
#include<string>
#include<iostream>
using namespace std;

struct Node
{
string name;
int old;
}POOL[10];
unordered_map<string, int> this_map; // khai bao nếu xác định 1 key 										//tương ứng với 1 Node

int main(){
POOL[0].name = "abc";
POOL[0].old = 18;
POOL[1].name = "abcd";
POOL[1].old = 19;

//xóa hết các phần tử trong map
this_map.clear(); 

//insert vào map
this_map["abc"] = 0; 
this_map["abcd"] = 1; 

//Tìm phần tử có với key là “abc”
auto iter = this_map.find("abc");


if(iter == this_map.end())   // không tìm được phần tử nào với key “abc”
(this_map.count(“abc”) = 0 or 1)  // 1 cách khác để xác định với key “abc” có tìm 									được phần tử nào hay k, 0 -> k có, 1 là có)
Int pool = *iter->second;


//xóa phần tử với key là “abc”
this_map.erase(“abc”) ||  this_map.erase(iter)

---
#include<unordered_map>
#include<string>
#include<iostream>
using namespace std;

struct Node
{
string name;
int old;
}POOL[10];
unordered_map<string, list<int>> this_map; // khai bao nếu xác định 1 key 										 //có thể tìm được nhiều node

int main(){
POOL[0].name = "abc";
POOL[0].old = 18;
POOL[1].name = "abcd";
POOL[1].old = 19;

//xóa hết các phần tử trong map
this_map.clear(); 

//insert vào map
this_map["abc"].push_back(0);
this_map["abc"].push_back(1);
//Tìm phần tử có với key là “abc”

auto iter = this_map.find("abc");

if(iter == this_map.end())   // không tìm được phần tử nào với key “abc”
(this_map.count(“abc” = 0 or 1)  // 1 cách khác để xác định với key “abc” có tìm 									được phần tử nào hay k, 0 -> k có, 1 là có)

for(auto i = iter->second.begin(); i != iter->second.end(); i++){
int mp = *i;
}
//xóa phần tử với key là “abc” va old = 18
for(auto i = iter->second.begin(); i != iter->second.end();){
int tmp = *i;
If(POOL[tmp]->old == 18){
	i = this_map.erase(i);
}else i++;
}


*** heap

#include<queue>
#include<string>
#include<iostream>
using namespace std;


struct Node
{
string name;
int old;
}POOL[10];

//Hàm compare để xác định max heap hay min heap
struct CP
{
bool operator() (Node* n1,Node* n2){
if( n2->old > n1->old) return true;
return false;
	}
};
//Khởi tạo heap
priority_queue<Node*,vector<Node*>,CP> heap;  
int main(){
POOL[0].name = “A";
POOL[0].old = 18;
POOL[1].name = “B";
POOL[1].old = 20;
//reset heap
heap = priority_queue<Node*,vector<Node*>,CP>();
//push phần tử vào heap
heap.push(&POOL[0]);
heap.push(&POOL[1]);

//Check size heap
Int a = heap.size();
while (!heap.empty())
{
Node *tmp = heap.top();
cout << tmp->old << tmp->name;
heap.pop();  //pop phần tử đầu
}

return 0;
}

*** set
#include<set>
#include<string>
#include<iostream>
using namespace std;


struct Node
{
string name;
int old;
}POOL[10];
struct CP
{
bool operator() (Node* n1,Node* n2){
if( n2->old > n1->old) return true;
return false;
}
};
set<Node*,CP> myset;

int main(){
POOL[0].name = "A";
POOL[0].old = 10;
POOL[1].name = "B";
POOL[1].old = 20;
POOL[2].name = "C";
POOL[2].old = 30;
//reset myset
myset.clear();
//insert phan tu
myset.insert(&POOL[0]);
myset.insert(&POOL[1]);
myset.insert(&POOL[2]);

//check size
Myset.size();

//Tìm phần tử trong set
Node *t = new Node();
t->old = 20;
auto iter = myset.find(t);

//Duyet các phân tử theo thứ tự
for(auto it = myset.begin(); it != myset.end();it++){
Node *tmp = *it;

cout << tmp->name << tmp->old << endl;

}
//xoa phần tử trong map
 auto iter = myset.find(t);
myset.erase(iter);


Lower_bound(): It returns an iterator pointing to the first element in the container 
which is not considered to go before val. (phần tử đầu tiên lớn hơn hoặc bằng)

Ex: 10 20 30 40 50 60 70 80                  lower_bound(30) = 30; lower_bound(31) = 40; 

upper_bound(): It returns an iterator pointing to the first element in the container which is considered to go after val (phần tử đầu tiên lớn hơn)

Ex: 10 20 30 40 50 60 70 80                  upper_bound(30) = 40; upper_bound(31) = 40; 
Editor is loading...
Leave a Comment