Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Vector thͱc ra nó cũng như mãng vɪy nhưng cách xài thì linh hoɞt hơn nhiɾu, và đây

là 1 s͑ điʀm n͕i tr͙i cͧa


vector so v͛i mãng :
- Bɞn không cɤn phɠi khai báo kích thư͛c cͧa mãng ví dͥ int A[100]..., vector có thʀ tͱ đ͙ng nâng kíck thư͛c lên.
- Nɼu bɞn thêm 1 phɤn tͭ vào vector đã đɤy r͓i, thì vector sɺ tͱ đ͙ng tăng kíck thư͛c cͧa nó lên đʀ dành ch͗ cho
giá trʈ m͛i này.- Vector còn có thʀ cho bɞn biɼt s͑ lưͣng các phɤn tͭ mà bɞn đang lưu trong nó.
- Dùng s͑ phɤn tͭ âm vɨn đưͣc trong vector ví dͥ A-10], A[-3], rɢt tiʄn trong viʄc cài đɴt các giɠi thuɪt cɤn kĩ
thuɪt lính canh.Y
Y Y
 Y

Đʀ có thʀ xài vectors thì bɞn phɠi thêm cho nó 1 header file sau :
PHP Code:
#include <vector>

và phɠi có
PHP Code:
using namespace std

Vì vector bɠn chɢt là thu͙c STL( Standard Template Library).

YY

Cú pháp cͧa vector cũng rɢt đơn giɠn ví dͥ :


PHP Code:
vector<int> A ;

Câu lʄnh trên đʈnh nghĩa 1 vector có kiʀu int. Chú ý kiʀu cͧa vector đưͣc đʀ trong 2 cái ngoɴc nh͍n. Vì kíck thư͛c
cͧa vector có thʀ nâng lên, cho nên không cɤn khai báo cho nó có bao nhiêu phɤn tͭ cũng đưͣc, hoɴc nɼu thích
khai báo thì bɞn cũng có thʀ khai báo như sau :
PHP Code:
vector<int> A(10);

Câu lʄnh trên khai báo A là 1 vector kiʀu int có 10 phɤn tͭ. Tuy nhiên như đã nói ͟ trên, mɴc dù size = 10, nhưng
khi bɞn add vào thì nó vɨn cho phép như thư͝ng.
Và ta cũng có thʀ kh͟i tɞo cho các phɤn tͭ trong vector bɮng cú pháp đơn giɠn như sau :
PHP Code:
vector<int> A(10, 2);

Trong câu lʄn trên thì 10 phɤn tͭ cͧa vector A sɺ đưͣc kh͟i tɞo bɮng 2.

Đ͓ng th͝i ta cũng có thʀ kh͟i tɞo cho 1 vector bɮng giá trʈ cͧa 1 vector khác, ví dͥ :
PHP Code:
vector<int> A(10,2);
vector<int> B(A);

V͛i dòng lʄnh trên thì vector B sɺ là bɠn sao cͧa vector A.
M͙t s͑ ví dͥ vɾ khai báo vector :
PHP Code:
vector<float> A;
vector<int> B(15);
vector<char> C(25, 'A');

Sau đây sɺ là 1 đoɞn code nh͏ mô tɠ cách xài vector :


c YY
PHP Code:
««Chương trình này sҿ lưu vào 2 vector "hours" và "payRate"
««5 ngưӟi employees và hour work cөa hӏ.
#include <iostream>
#include <iomanip>
#include <vector> «« Nhӝ cái này nha !!
using namespace std; «« Cái này nӱa

int main()
{
const int NUM_EMPLOYEES = 5; «« Number of employees
vector<int> hours(NUM_EMPLOYEES); «« Mӛt vector hours kiӅu int
vector<double> payRate(NUM_EMPLOYEES); «« Mӛt vector payRate kiӅu
««double
int index;

«« Nhүp dӱ liӉu
cout << "Enter the hours worked by " << NUM_EMPLOYEES;
cout << " employees and their hourly rates.\n";
for (index = 0; index < NUM_EMPLOYEES; index++)
{
cout << "Hours worked by employee #" << (index + 1);
cout << ": ";
cin >> hours[index];
cout << "Hourly pay rate for employee #";
cout << (index + 1) << ": ";
cin >> payRate[index];
}

«« In ra dӱ liӉu nhүp vào


cout << "\nHere is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
for (index = 0; index < NUM_EMPLOYEES; index++)
{
double grossPay = hours[index] * payRate[index];
cout << "Employee #" << (index + 1);
cout << ": $" << grossPay << endl;
}
return 0;
}

Ou u Y
Code:

Enter the hours worked by 5 employees and their hourly rates.


Hours worked by employee #1: ë 
Hourly pay rate for employee #1: 

Hours worked by employee #2:ë 
Hourly pay rate for employee #2: 
Hours worked by employee #3:  
Hourly pay rate for employee #3:  
Hours worked by employee #4: ë

Hourly pay rate for employee #4: Hours worked by employee #5:  
Hourly pay rate for employee #5: ë 

Here is the gross pay for each employee:


Employee #1: $97.50
Employee #2: $129.30
Employee #3: $800.00
Employee #4: $13.50
Employee #5: $626.00
Press any key to continue . . .

Chú ý vɾ chương trình trên là cɠ 2 vector đɾu có size là 5, và chương trình sɺ dùng vòng lɴp for đʀ lưu các giá trʈ
cͧa cɠ 2 vector :
PHP Code:
for (index = 0; index < NUM_EMPLOYEES; index++)
{
cout << "Hours worked by employee #" << (index + 1);
cout << ": ";
cin >> hours[index];
cout << "Hourly pay rate for employee #";
cout << (index + 1) << ": ";
cin >> payRate[index];
}

Y
Y
cYY YY Y
YY
 YuY

Chú ý bɞn không thʀ dùng toán tͭ [] đʀ truy xuɢt các phɤn tͭ mà nó không t͓n tɞi, nghĩa là ví dͥ vector size = 10,
mà bɞn truy xuɢt 11 là banh xác. Đʀ thêm vào 1 giá trʈ cho vector mà nó không có size trư͛c hoɴc đã full thì ta
dùng hàm thành viên "push_back". Ví dͥ :
PHP Code:
A.push_back(25);

V͛i câu lʄnh trên thì giɠ sͭ ta đang có 1 vector A kiʀu int, v͛i size bao nhiêu đó, nɼu nó chưa full thì 25 sɺ thay cho
phɤn tͭ cu͑i cùng. Còn nɼu không full thì nó sɺ tɞo 1 ch͗ m͛i dành cho thɮng 25 này. Đoɞn code dư͛i đây sɺ mô
tɠ cách dùng cͧa hàm push_back này :
c YY 
PHP Code:

#include <iostream>
#include <iomanip>
#include <vector> «« Needed to define vectors
using namespace std;

int main()
{
vector<int> hours; «« hours bây h là vector rәng
vector<double> payRate; «« payRate cũng là 1 vector rәng
int numEmployees;
int index;

«« Lҧy sӓ lưӥng employees


cout << "How many employees do you have? ";
cin >> numEmployees;

«« Nhүp dӱ liӉu
cout << "Enter the hours worked by " << numEmployees;
cout << " employees and their hourly rates.\n";
for (index = 0; index < numEmployees; index++)
{
int tempHours; «« To hold the number of hours entered
double tempRate; «« To hold the payrate entered

cout << "Hours worked by employee #" << (index + 1);


cout << ": ";
cin >> tempHours;
hours.push_back(tempHours); «« Thêm phҩn tӯ vào vector hours.
cout << "Hourly pay rate for employee #";
cout << (index + 1) << ": ";
cin >> tempRate;
payRate.push_back(tempRate); «« Thêm phҩn tӯ vào vector payRate
}

««In ra giá trӍ


cout << "Here is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
for (index = 0; index < numEmployees; index++)
{
double grossPay = hours[index] * payRate[index];
cout << "Employee #" << (index + 1);
cout << ": $" << grossPay << endl;
}
return 0;
}

Ou u
Code:

How many employees do you have?


Enter the hours worked by 3 employees and their hourly rates.
Hours worked by employee #1:  
Hourly pay rate for employee #1: ë 
Hours worked by employee #2:  
Hourly pay rate for employee #2: ë  
Hours worked by employee #3:  
Hourly pay rate for employee #3:  

Here is the gross pay for each employee:


Employee #1: $505.20
Employee #2: $258.75
Employee #3: $1019.25
Press any key to continue . . .

c!Y"#Y Y $%YY


 Y & Y'uYY( )*

Không gi͑ng v͛i mãng, dùng vector ta có thʀ thông báo đưͣc kíck thư͛c hiʄn th͝i mà nó đang có v͛i cú pháp cͱc
kì đơn giɠn như sau :
PHP Code:
int numberValues = A.size()

c YY+
PHP Code:
#include <iostream>
#include <vector>
using namespace std;

«« Function prototype
void showValues(vector<int>);

int main()
{
vector<int> values;

«« Thêm vào vector values 7 giá trӍ


for (int count = 0; count < 7; count++)
values.push_back(count * 2);

««In ra kӁt quҥ


showValues(values);
return 0;
}

««**************************************************
«« Chú ý đӍnh nghĩa cөa hàm showValues *
«« Hàm này nhүn vector kiӅu int là đӓi, và giá trӍ *
«« cөa 1 phҩn tӯ sҿ đưӥc in ra * *
««**************************************************

void showValues(vector<int> vect)


{
for (int count = 0; count < vect.size(); count++)
cout << vect[count] << endl;
}

Ou u
Code:

0
2
4
6
8
10
12
Press any key to continue . . .

c ! YY,Y Y   Y
 Y- YY Y
Y 
Cú pháp cũng đơn giɠn như sau :
PHP Code:
A.pop_back();

Và sau đây là ví dͥ vɾ cách xài :

YY.
PHP Code:
«« Chương trình này mô tҥ cách xài cөa hàm pop_back()
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> values;

««Thêm dӱ liӉu vào vector values.


values.push_back(1);
values.push_back(2);
values.push_back(3);
cout << "The size of values is " << values.size() << endl;

«« oá là đây!!.
cout << "Popping a value from the vector...\n";
values.pop_back();
cout << "The size of values is now " << values.size() << endl;

«« oá tiӁp.
cout << "Popping a value from the vector...\n";
values.pop_back();
cout << "The size of values is now " << values.size() << endl;

«« oá nӱa, xoá cho hӁt thì thôi ^^.


cout << "Popping a value from the vector...\n";
values.pop_back();
cout << "The size of values is now " << values.size() << endl;
return 0;
}

Y
Ou u
Code:

The size of values is 3


Popping a value from the vector...
The size of values is now 2
Popping a value from the vector...
The size of values is now 1
Popping a value from the vector...
The size of values is now 0
Press any key to continue . . .

c Y/ YY Y


Y0 )*Y"1Y2 Y3Y4YY,Y YY
 
Nɼu mu͑n diʄt c͏ tɪn g͑c thì dùng hàm clear v͛i cú pháp như sau :
PHP Code:
A.clear();

c YY5
PHP Code:
«« Mô tҥ cách dùng hàm clear().
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> values(100);

cout << "The values vector has "


<< values.size() << " elements.\n";
cout << "I will call the clear member function...\n";
values.clear();
cout << "Now, the values vector has "
<< values.size() << " elements.\n";
return 0;
}
Code:

The values vector has 100 elements.


I will call the clear member function...
Now, the values vector has 0 elements.
Press any key to continue . . .

c Y/ YY Y


Y  6)*Y"1Y1Y Y2 Y
 Y7Y8 Y6Y&

Đʀ xác đʈnh vector có r͗ng hay không ta dùng hàm thành viên empty(), hàm này trɠ vɾ true nɼu vector r͗ng, và
false ngưͣc lɞi. Cú pháp :
PHP Code:
if(A.empty() == true){
cout << "No values in A \n";
}

c YY9
PHP Code:
«« This program demonstrates the vector's empty member function.
#include <iostream>
#include <vector>
using namespace std;

«« Function prototype
double avgVector(vector<int>);

int main()
{
vector<int> values; «« Khai báo vector values kiӅu int
int numValues; «« sӓ lưӥng pҩn tӯ
double average; «« biӁn lưu giá trӍ trung bình

«« Lҧy sӓ lưӥng phҩn tӯ đӅ tính trung bình


cout << "How many values do you wish to average? ";
cin >> numValues;

«« Lưu giá trӍ vào vector.


for (int count = 0; count < numValues; count++)
{
int tempValue;
cout << "Enter a value: ";
cin >> tempValue;
values.push_back(tempValue);
}

«« Tính giá trӍ trung bình và in ra.


average = avgVector(values);
cout << "Average: " << average << endl;
return 0;
}

««*************************************************************
«« Hàm argVector này sҿ nhүn 1 đӓ sӓ là 1 vector *
«« kiӅu int. hàm này trҥ vӃ giá trӍ trung bình cөa các phҩn tӯ *
«« cөa vector. NӁu vector rәng thì lәi sҿ đưӥc báo,
««và hàm trҥ vӃ 0.0 *
««*************************************************************

double avgVector(vector<int> vect)


{
int total = 0; ««Tӗng
double avg; «« giá trӍ trung bình

if (vect.empty()) ««kiӅm tra xem vector có rәng không


{
cout << "No values to average.\n";
avg = 0.0;
}
else
{
for (int count = 0; count < vect.size(); count++)
total += vect[count];
avg = total « vect.size();
}
return avg;
}

Ou u Y
Code:

How many values do you wish to average? 


Enter a value: ë
Enter a value: ë 
Enter a value: 
Enter a value:

Enter a value: 
Average: 9
Press any key to continue . . .

Ou u Y
Code:

How many values do you wish to average? 


No values to average.
Average: 0
Press any key to continue . . .

O Y:YYY
Y;Y<
Y
=Y ) 0   * : Trɠ vɾ giá trʈ cͧa phɤn tͭ thͩ element cͧa vector. Ví dͥ :
PHP Code:
x = A.at(5);

-  6)* : Trɠ vɾ s͑ lưͣng các phɤn tͭ mà vector đang lưu trͯ. Ví dͥ :
PHP Code:
x = A.capacity();

- 
 )* : Đɠo thͩ tͱ cͧa các phɤn tͭ( phɤn tͭ đɤu thành phɤn tͭ cu͑i...) Ví dͥ :
PHP Code:
A.reverse();

- >)
  *? : Đ͕i ch͗ n͙i dung cͧa vector1 v͛i vector2. Ví dͥ :
PHP Code:
vect1.swap(vect2);

-  ( ) 0   @Y
0u * : Đʈnh nghĩa lɞi kíck thư͛c bɮng elements, m͗i elements này sɺ đưͣc kh͟i tɞo v͛i giá
trʈ là value. Ví dͥ :
PHP Code:
A.resize(5,1);

You might also like