/////////////////Vector căn cản/////////////

//////////////////////////////////Part 1////////////////////////////////////////// #include<iostream> using namespace std; #include<vector>// Khai báo thư viện sử dụng vector int main() { // Cách Khai Báo mãng vector arr vector<int> arr;// Đặt tên cho vector // Có 2 cách để biểu diễn vector // 1. là xem vector như 1 con trỏ ta cấp phát động cho nó trước //vd: //vector<int> arr1; int n=5; //arr.resize(n);// cấp phát trước bộ nhớ //arr1.resize(n);// cấp phát trước bộ nhớ //arr1[2]=100; //arr1[3]=101; //arr1[4]=102; //1.1 //arr[0]=1; //arr[1]=2; //arr[2]=3; //arr[3]=4; //arr[4]=5; //cout<<arr[0]<<arr[1]<<arr[2]<<arr[3]<<arr[4]; //1.2 cout<<sizeof(arr)<<endl;// kích thươc bytes vector for(int i=0;i<n;i++) { arr.push_back(i);/// cấp phát tùy số lượng vector arr[i]=i+1; } //arr.push_back(9);// thêm vào cuối vector //arr.insert(arr.begin()+1,arr1.begin()+2,arr1.begin()+4); //arr.erase(arr.begin()+1,arr.begin()+4); int size =arr.size();// lấy số lượng phần tử của vector đang chứa cout<<size<<endl; for(int i=0;i<size;i++) { cout<<arr[i]<<" "; } cout<<"\nKich thuoc that su vector dang luu tru!"<<arr.capacity(); system("pause"); return 0; } ////Bên con trỏ thì cuối cùng sau khi ta cấp phát bộ nhớ cho nó thì ta phải giải phóng cho nó vì nó nằm trong vùng nhớ heap! //Còn bên vector thì tự động nó sẽ giải phóng nó ! yên tâm nhé lập trình viên gà //////////////////////////////////Part 2///////////////////////////////////

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.