C++ Vectors
Description
It stores data in an array but can dynamically change in size. Adding and removing of data are usually done at the end. Data can be accessed by index.
விரிவுரை
இது தரவுகளைச் வரிசையாக சேமிக்கிறது, ஆனால் அளவில் மாறும் வகையில் மாறக்கூடும். தரவுகளைச் சேர்ப்பதும் நீக்குவதும் பொதுவாக இறுதியில் செய்யப்படும். தரவுகளை குறியீட்டு மூலம் அணுகலாம்.
Both vectors and arrays are data structures used to store multiple elements of the same data type.
The difference between an array and a vector, is that the size of an array cannot be modified (you cannot add or remove elements from an array). A vector however, can grow or shrink in size as needed.
To use in C++ Program we have to include header tag <vector>
வெக்டர்கள் மற்றும் அணிவரிசைகள் இரண்டும் ஒரே தரவு வகையின் பல கூறுகளைச் சேமிக்கப் பயன்படுத்தப்படும் தரவு கட்டமைப்புகள் ஆகும்.
ஒரு அணிக்கும் ஒரு திசையனுக்கும் உள்ள வேறுபாடு என்னவென்றால், ஒரு அணிவரிசையின் அளவை மாற்ற முடியாது (ஒரு அணிவரிசையிலிருந்து கூறுகளைச் சேர்க்கவோ நீக்கவோ முடியாது). இருப்பினும், ஒரு திசையன் தேவைக்கேற்ப அளவு வளரவோ அல்லது சுருங்கவோ முடியும்.
ஒரு வெக்டரைப் பயன்படுத்த, நீங்கள் <வெக்டர்> தலைப்பு கோப்பைச் சேர்க்க வேண்டும்:
Syntax
// Include the vector library
#include <vector>
Create a Vector / வெக்டர் உருவாக்க
To create a vector, use the vector keyword, and specify the type of values it should store within angle brackets <> and then the name of the vector, like: vector<type> vectorName.
ஒரு வெக்டரை உருவாக்க, vector முக்கிய சொல்லைப் பயன்படுத்தி, கோண அடைப்புக்குறிக்குள் சேமிக்க வேண்டிய மதிப்புகளிவகையைக் குறிப்பிடவும், பின்னர் வெக்டரின் பெயரைக் குறிப்பிடவும், அதாவது: vector<type> vectorName.
Example / உதாரணம்
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector called cars that will store strings
vector<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};
// Print vector data
for (string food : foods) {
cout << food << “\n”;
}
return 0;
}
Output:
Idly
Pongal
Vadai
Dosa
Access a Vector
You can access a vector element by referring to the index number inside square brackets [].
Vectors, like arrays, are 0-indexed, meaning that [0] is the first element, [1] is the second element, and so on:
ஒரு வெக்டரை அணுகவும்
சதுர அடைப்புக்குறிக்குள் உள்ள குறியீட்டு எண்ணைக் குறிப்பிடுவதன் மூலம் நீங்கள் ஒரு வெக்டர் உறுப்பை அணுகலாம் [].
அணிவரிசைகளைப் போலவே வெக்டரும் 0-குறியீடு செய்யப்படுகின்றன, அதாவது [0] முதல் உறுப்பு, [1] இரண்டாவது உறுப்பு, மற்றும் பல:
Example / உதாரணம்
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector called cars that will store strings
vector<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};
// Print vector data on index 0
cout << foods[0];
// Print vector data on index 2
cout<< foods[2];
return 0;
}
Output:
IdlyVadai
One advantage of using the vector library, is that it includes many useful functions. For example, you can access the first or the last element of a vector with the .front() and .back() functions:
வெக்டார் நூலகத்தைப் பயன்படுத்துவதன் ஒரு நன்மை என்னவென்றால், அது பல பயனுள்ள செயல்பாடுகளை உள்ளடக்கியது. எடுத்துக்காட்டாக, .front() மற்றும் .back() செயல்பாடுகளைப் பயன்படுத்தி ஒரு வெக்டரின் முதல் அல்லது கடைசி உறுப்பை அணுகலாம்:
To access an element at a specified index, you can use the .at() function and specify the index number:
ஒரு குறிப்பிட்ட குறியீட்டில் ஒரு உறுப்பை அணுக, நீங்கள் .at() செயல்பாட்டைப் பயன்படுத்தி குறியீட்டு எண்ணைக் குறிப்பிடலாம்:
Example / உதாரணம்
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector called cars that will store strings
vector<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};
// Print vector data front and last
cout << foods.front() << “\n”;
cout << foods.back() << “\n”;
// Print vector data with at index 2
cout<< foods.at(2);
return 0;
}
Output:
Idly
Dosa
Vadai
Exercise / பயிற்சி
To change the value of a specific element, you can refer to the index number:
- Change the value of “Pongal” to “Adai”
- Use the function .at() to change value “Dosa” to “RavaDosa”
Add data to Vector / வெக்டரில் தரவைச் சேர்க்கவும்
The biggest difference between a vector and an array is that vectors can grow dynamically. That means you can add or remove elements from the vector.
To add an element to the vector, you can use the .push_back() function, which will add an element at the end of the vector:
ஒரு வெக்டருக்கும் ஒரு அணிவரிசைக்கும் உள்ள மிகப்பெரிய வித்தியாசம் என்னவென்றால், வெக்டார்கள் மாறும் வகையில் வளர முடியும். அதாவது நீங்கள் வெக்டரிலிருந்து கூறுகளைச் சேர்க்கலாம் அல்லது அகற்றலாம்.
வெக்டரில் ஒரு உறுப்பைச் சேர்க்க, நீங்கள் .push_back() செயல்பாட்டைப் பயன்படுத்தலாம், இது வெக்டரின் இறுதியில் ஒரு உறுப்பைச் சேர்க்கும்:
Example / உதாரணம்
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector called cars that will store strings
vector<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};
foods.push_back(“Puri”);
// Print vector data
for (string food : foods) {
cout << food << “\n”;
}
return 0;
}
Output:
Idly
Pongal
Vadai
Dosa
Puri
Remove Vector Data
To remove a data from the vector, you can use the .pop_back() function, which removes the data from the end of the vector:
Syntax
vector<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};
foods.pop_back();
Exercise / பயிற்சி
Try to add following datas “RavaDosa” , “MasalaDosa” , “Kesari” to foods using push_back() option and try to use pop_back() to remove the last item and print all foods at end
Vector Size
To find out how many data a vector has, use the .size() function:
ஒரு திசையன் எத்தனை தரவுகளைக் கொண்டுள்ளது என்பதைக் கண்டறிய, .size() செயல்பாட்டைப் பயன்படுத்தவும்:
Syntax
vector<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”,”RavaDosa”};
foods.size();
Output:
5
Check if a Vector is Empty
There is also a function to find out whether a vector is empty or not.
The .empty() function returns 1 (true) if the vector is empty and 0 (false) if it contains one or more datas:
ஒரு வெக்டர் காலியாக உள்ளதா என சரிபார்க்கவும்
ஒரு வெக்டர் காலியாக உள்ளதா இல்லையா என்பதைக் கண்டறிய ஒரு செயல்பாடும் உள்ளது.
.empty() செயல்பாடு, வெக்டர் காலியாக இருந்தால் 1 (true) ஐயும், ஒன்று அல்லது அதற்கு மேற்பட்ட தரவுகளைக் கொண்டிருந்தால் 0 (false) ஐயும் வழங்குகிறது: