0% found this document useful (0 votes)
10 views18 pages

Hướng Dẫn Sử Dụng Vector Trong C++

Uploaded by

ngtrmyy11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Hướng Dẫn Sử Dụng Vector Trong C++

Uploaded by

ngtrmyy11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VECTOR – Mảng động

Con trỏ thông minh trỏ đến 1 phần tử nào đó

Ta có thể thông qua iterator để truy cập vào giá trị của pt mà nó đang trỏ tới

Dùng auto rất hay vì nó có thể thay việc phải khai báo cho các iterator , số , string…

#include <bits/stdc++.h>

using namespace std;

//push_back

// size

// truy cap cac pt trong vector thong qua chi so

// duyet thong qua chi so

// for each

// duyet su dung iterato

int main(){

vector <int> v; // khai bao 1 vetor rong

v.push_back(2);

v.push_back(6);

cout << [Link]() << endl;

v.push_back(8);

for (int i = 0; i < [Link](); i++) cout << v[i] <<' '; // truy cap cac pt trong vt bang chi so

cout << endl;

for (int x : v){

cout << x << ' ';

cout << endl;

// duyet su dung iterator

// khai bao iterator // tro den pt dau tien - chua tro den pt sau pt cuoi cung

for (vector <int> :: iterator it = [Link](); it != [Link](); ++it){

cout << *it << ' ';

cout << endl;

// duyet su dung auto

for (auto it = [Link](); it != [Link](); ++it){

cout << *it << ' ';


}

cout << endl;

// su dung iterator de truy cap den 1 pt co chi so 2

cout << *([Link]()+2);

return 0;

Trong trường hợp sử dụng vetor như là mảng có n phần tử

vetor <int> v(n); // khai bao san vector co n phan tu v[n]

for (int i = 0; i < n; i++) cin >> v[i];

Trong trường hợp không khai báo sẵn số lượng phần tử mà muốn nhập vào 1 mảng thì

vector <int> v;

for (int i = 0; i < n; i++){

int x; cin >> x;

v.push_back(x);

Khai báo và khởi tạo giá trị cho các pt của vector

vector <int> b(n,100);

for (int i = 0; i < n ; i++) cout << b[i] << ' ';

Tách từ trong xâu

#include <bits/stdc++.h>

using namespace std;

int main(){

string s = " hello vu kiet school";

stringstream ss(s);

string tmp;

vector <string> v;

while (ss >> tmp){

v.push_back(tmp);

for (string x: v){

cout << x << ' ';

return 0;

}
Sắp xếp vector

#include <bits/stdc++.h>

using namespace std;

bool cmp(string a, string b){

if ([Link]() != [Link]())

return [Link]() > [Link]();

else

return a < b;

// sapxep cac tu theo thu tu chieu dai, trong th 2 tu co cung chieu dai

// tu nao co thu tu tu dien nho hon thi in truoc

int main(){

// sap xep mang: sort(a,a+n) sort(a+x, a+ y + 1) sx tu chi so a[x] den chi so a[y-1]

// sap xep

// sap xep vector

// vector <int> v(5);

// for (int i = 0; i < 5; i++) cin >> v[i];

// sort([Link](),[Link]()); // sort([Link]() + x, [Link]() + y) sx tu chi so a[x] den chi so a[y-1]

// for (int i = 0; i < [Link](); i++) cout << v[i] << ' ';

// sx cac tu trong xau

//int n ; cin >> n;

//string a[n];

//for (int i=0; i < n; i++) cin >> a[i];

//sort(a, a+n);

//for (int i = 0; i < n; i++) cout << a[i] << '\n';

// sx cac ki tu trong xau

// string s;

// getline(cin,s);

// sort([Link](), [Link]());

// cout << s;

// pair

// int n; cin >> n;


// pair <int , int> a[n];

// for (int i = 0; i < n; i++) {

// cin >> a[i].first >> a[i].second;

// }

// sort(a,a+n);

// for (auto it: a){

// cout << [Link] << ' ' << [Link] << endl;

// }

return 0;

***

Vector 2 chiều

#include <bits/stdc++.h>;

using namespace std;

int main(){

int n, m; cin >> n >> m;

vector <vector<int>> v(n,vector<int>(m));

for (int i = 0; i < n; i++){

for (int j = 0; j < m; j++) cin >> v[i][j];

for (int i = 0; i < n; i++){

for (int j = 0; j < m; j++) {

cout << v[i][j];

cout << endl;

}
Bài tập:

#include <bits/stdc++.h>

using namespace std;

//bool cmp(int a, int b){

// return a < b;

//}

bool cmp(string a, string b){

if ([Link]() != [Link]()) return [Link]() > [Link]();

else

return a < b;

int main(){

string s;

getline(cin,s);

vector <string> v;

stringstream ss(s);

string tmp;

while (ss >> tmp){

v.push_back(tmp);

//cout << tmp << endl;

sort([Link](), [Link](),cmp);

for (int i = 0; i < [Link](); i++) cout << v[i] << endl;

for (auto x: v) cout << x << endl;

// vector <int> nb;

// s = s + "abc";

// int ans = 0, sum = 0;

// for (int i = 0; i < [Link](); i++){

// if (isdigit(s[i])){

// sum = sum * 10 + s[i] - '0';

// }

// else

// {

// if (sum != 0) nb.push_back(sum);
// sum = 0;

// }

// }

// sort([Link](), [Link](),cmp);

// for (auto x: nb) cout << x << ' ';

return 0;

PAIR
#include <bits/stdc++.h>

using namespace std;

bool cmp(pair <int,int> a, pair<int,int> b){

if ([Link] != [Link] )

return [Link] > [Link];

return [Link] > [Link];

int main(){

// pair <int, int> a[100];

// int n; cin >> n;

// for (int i = 0; i < n; i++)

// cin >> a[i].first >> a[i].second;

// for (int i = 0; i < n; i++)

// cout << a[i].first << ' ' << a[i].second << endl;

// 1 thanh phan cua vector là 1 pair

// vector <pair<int, int>> v;

// int n; cin >> n;

// for (int i = 0; i<n; i++){

// int x, y; cin >> x >> y;

// v.push_back({x,y});

// }

// for (int i = 0; i < n; i++)

// cout << v[i].first << ' ' << v[i].second << '\n';

// // for each
// for (pair <int, int> x: v){

// cout << [Link] << ' ' << [Link] << '\n';

// }

// // auto

// for (auto it:v){

// cout << [Link] << ' ' << [Link] << '\n';

// }

// // set

// set <pair<int,int>> se;

// [Link]({1,2});

// [Link]({3,4});

// [Link]({1,1});

// [Link]({1,2});

// for (auto it: se)

// cout << [Link] << ' ' << [Link] << '\n';

// SAP XEP PAIR

pair <int, int> a[5];

a[0] = {1,2};

a[1]= {3,4};

a[2] = {1,1};

a[3] = {3,2};

a[4] = {4,5};

sort(a,a+5,cmp);

for (auto it: a){

cout << [Link] << ' ' << [Link] <<'\n';

// sap xep cac pair theo first giam dan neu 2 pair cùng first thì sx second giam dan

return 0;

BÀI TẬP

#include <bits/stdc++.h>

using namespace std;

//bool cmp(pair<int, int> a, pair <int ,int> b){

// return [Link] < [Link];


//}

bool cmp(pair <int, int> a, pair<int , int> b){

if ([Link] == [Link]) return [Link] > [Link];

return [Link] < [Link];

int main(){

// pair <int , int> p;

// cin >> [Link] >> [Link];

// cout << [Link] << ' ' << [Link];

int n; cin >> n;

pair <int , int> v[n];

for (int i = 0; i < n; i++)

cin >> v[i].first >> v[i].second;

// sap xep pair

sort(v,v+n,cmp);

for (int i = 0; i < n; i++)

cout << v[i].first << ' ' << v[i].second << '\n';

// khong khai bao 3 gia tri

// pair <int , pair<int ,int>> a[n];

// for (int i = 0; i < n; i++){

// cin >> a[i].first >> a[i].[Link] >> a[i].[Link];

// }

//

// // sap xem pair

//

// for (int i = 0; i < n; i++){

// cout << a[i].first << ' '<< a[i].[Link] << ' ' << a[i].[Link] <<'\n';

// }

return 0;

}
MAP
#include <bits/stdc++.h>

using namespace std;

int main(){

// map<int, int> mp;

// mp[100] = 200; // them cap (key, value) vao trong map

// mp[-2] = 300;

// [Link]({300,400});

// mp[100] = 100; // (100,300) // khi thay doi gia tri cua 1 key nao do thì key do van ton tai

// cout << [Link]() << endl; // chi thay gia tri chu no se k them 1 cap key value

// // duyet map

// for (pair <int, int> x : mp){

// cout << [Link] << ' ' << [Link] << endl;

// }

// for (auto it: mp){

// cout << [Link] << ' ' << [Link] << endl;

// }

// for (map<int ,int> :: iterator it = [Link](); it != [Link](); it++){

// cout << (*it). first << ' ' << (*it).second << endl;

// }

// // tim kiem theo key count or find

// if ([Link](100)!= 0)

// cout << "Found";

// else

// cout << "Not Found";

// if ([Link](100)!= [Link]())

// cout << "Found";

// else

// cout << "Not Found";

// [Link](100); // xoa ca cap key value co key 100 luon

// for (auto it: mp){

// cout << [Link] << ' ' << [Link] << endl;

// }
// thuong dung dem so lan xuat hien cua cac pt trong mang - in ra tan xuat

// 8

// 1 1 2 1 3 4 -5 2

// map <int, int> mp;

// int n; cin >> n;

// for (int i = 0; i < n; i++){

// int x; cin >> x;

// mp[x]++;

// }

// // cac pt trong map no sx tang dan theo key

// for (auto it:mp){

// cout << [Link] << ' ' << [Link] << endl;

// }

// //Neu phai in theo thu tu xuat hien

// map <int , int> mp;

// int n; cin >> n;

// int a[1000];

// for (int i = 0; i < n; i++){

// cin >> a[i];

// mp[a[i]]++;

// }

// for (int i = 0; i < n; i++){

// if (mp[a[i]] != 0){ // hoac in xong thi erase

// cout << a[i] << ' ' << mp[a[i]] << endl;

// mp[a[i]] = 0;

// }

// }

// dem xem tu nao xuat hien nhieu nhat

map <string, int> mp;

int n; cin >> n;

for (int i = 0; i< n; i++){

string s; cin >> s;

mp[s]++;

}
for (auto it : mp){

cout << [Link] << ' ' << [Link] << endl;

// tan suat xuat hien nhieu nhat

int max_fre = 0;

string res;

for (auto it: mp){

if ([Link] > max_fre){ // neu in thang co thu tu tu dien lon thì them dau =

max_fre = [Link];

res = [Link];

cout << res << max_fre;

return 0;

SET
#include <bits/stdc++.h>

using namespace std;

int main(){

// set duy tri thu tu tu nho den lon

// cac pt trong set la rieng biet (pt nao da co trong set thi nó k luu them vao nua

// set<int> s;

// [Link](100);

// [Link](200);

// [Link](300);

// [Link](100);

// cout << [Link]();

// tim kiem 1pt có trong set k O(logN)

// set <int> s;

// for (int i = 0; i < 10; i++) [Link](i);

// cout << [Link]() << endl;

// if ([Link](5) != 0) cout << "Found" << endl;

// else
// cout << "NO Found" << endl;

// // find tra ve iterator tro den pt trong set, neu pt tim kiem k co trong set thi no tra ve

// // [Link]() - iterator pt sau pt cuoi cung

// if ([Link](99) != [Link]()) cout << "Found";

// else

// cout << "NO Found";

// // Erase O(logN)

// [Link](5); // hoac [Link]([Link](5));

//

// cout << endl;

// duyet set

// for (int x:s){

// cout << x << ' ';

// }

// cout << endl;

// for (auto x : s){

// cout << x << ' ';

// }

// cout << endl;

// for (set<int> :: iterator it = [Link](); it != [Link](); it++)

// cout << *it << ' ';

// cout << endl;

// truy cap toi pt dau tien trong set

// cout << *[Link]() << endl;

// cout << *[Link]() << endl;

// Nhap vao mang va dem so luong pt khac nhau

// int i; cin >> n;

// set<int> s;

// for (int i = 0; i < n; i++){

// int x; cin >> x;

// [Link](x);

// }

// cout << [Link]();

// Nhap vao 1 xau dem xem xau co bao nhieu tu khac nhau
// int n; cin >> n;

// set <string> s;

// for (int i = 0; i < n; i++){

// string st;

// getline(cin, st);

// [Link](st);

// }

// cout << [Link]() << endl;

// for (string x:s){

// cout << x << endl;

// }

// multiset cho phep cac pt trong set no co the giong nhau

// multiset <int> ms;

// [Link](100);

// [Link](200);

// [Link](100);

// for (int x: ms) cout << x << ' ';

// cout << endl;

// cout << [Link](100);

// chu y neu [Link](100) thi no se xoa het cac pt 100

// muon xoa 1 pt 100 thi xoa thong qua iterator

// BT cho mang co n pt và so nguyen k, doi voi moi day con lien tiep co chieu dai k phai in ra

// pt lon nhat, nho nhat

// 10 3

//1 2 3 1 4 5 1 8 9 10

// output

// 3 3 4 5 5 8 9 10

//int n, k; cin >> n >> k;

//int a[n];

//for (int &x: a) cin >> x;

//multiset <int> ms;

//for (int i = 0; i < k; i++) [Link](a[i]);

//for (int i = k; i < n; i++){

// cout << *[Link]() << ' '; // in ra pt lon nhat


// [Link]([Link](a[i-k])) ;// xoa di thang dau tien cua cua so - k muon xoa tat

// [Link](a[i]);

//}

//cout << *[Link]() << endl;

// unorderedd_set - khong co thu tu tu be den lon nua

unordered_set <int> s;

for (int i = 0; i < 10; i++) [Link](i);

for (int x: s) cout << x << endl;

return 0;

}
STACK – Ngăn xếp (LIFO)
#include <bits/stdc++.h>
using namespace std;
int main(){
// stack <int> s; // char ,string , long long...
// [Link](2);
// [Link](8);
// [Link](9);
// cout << [Link]() << endl;
// [Link]();
// cout << [Link]() << endl;
// cout << [Link]() << endl;
// if ([Link]()!= 0) cout << " NO RONG";
// else
// cout << "Rong";
// bai tap
// chuyen mot so tu he thap phan sang he nhi phan
int n; cin >> n;
stack <int> s;
while (n!=0){
int du = n % 2;
[Link](du);
n /= 2;
}
while ([Link]()==false){
cout << [Link]();
[Link]();
}
return 0;
}
// Cho một dãy n các dấu ngoặc “(“ “)”. Kiểm tra xem dãy đã cho có phải là dãy
ngoặc hợp lệ không? Nếu hợp lệ in ra YES, ngược lại in NO
Một dãy ngoặc hợp lệ khi:
+ Xâu rỗng là 1 dãy ngoặc hợp lệ
+ (): là 1 dãy ngoặc hợp lệ
+ Nếu A và B là dãy ngoặc hợp lệ thì (A) và AB cũng là 2 dãy ngoặc hợp lệ.
Input Output
(()) YES
()(()( NO
string s; cin >> s;
stack <char> st;
bool res = true;
for (int i = 0; i < [Link](); i++){
if (s[i] =='(') [Link](s[i]);
else
{
if ([Link]() == true) {
cout << "NO \n";
return 0;
}
else
[Link]();
}
}
if ([Link]() == true) cout << "YES";
else
cout << "NO";
return 0;
QUEUE – Hàng đợi (FIFO)
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
[Link](0); [Link](0);
queue <int> q;
[Link](1);
[Link](2);
[Link](3);
cout << [Link]() << endl; // in pt o dinh hang doi
[Link](); // xoa pt o dinh
cout << [Link]() << endl;
if ([Link]() == true) cout << "YES";
else
cout << "NO";

return 0;
}
Bài tập:
Hôm nay đi học, Sói được học bài học về hoán đổi dãy số. Cô giáo cho Sói một
dãy số bất kì và yêu cầu Sói hãy chuyển số hạng đầu tiên về cuối cùng. Điều này quả
thật là dễ dàng với Sói. Nhưng là một cậu bé ham học hỏi. Sói tự nghĩa xem nếu chuyển
đổi lên tục như vậy trong K lần thì dãy số mới sẽ là dãy nào?
Input
- Dòng đầu tiên gồm 2 số N và K (1 ≤ 𝑁 ≤ 104 , 1 ≤ 𝐾 ≤ 109 )
- Dòng tiếp theo gồm N số nguyên dương trong phạm vi int
Output
Ghi ra dãy số sau khi biến đổi
Ví dụ:
Input Output
53 45123
12345
int n, k; cin >> n >> k;
queue <int> q;
k = k % n;
for (int i = 0; i < n; i++){
int x; cin >> x;
[Link](x);
}
for (int i = 0; i < k ; i++){
int x = [Link]();
[Link]();
[Link](x);
}
for (int i = 0; i < n; i++){
cout << [Link]() << ' ';
[Link]();
}

You might also like