0% found this document useful (0 votes)
9 views

Lecture 5

Uploaded by

nerf quta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 5

Uploaded by

nerf quta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Pair and Vector

Greetings, Dear students. I hope you are doing great and are ready to learn new data types,
which will be very useful for creating your course projects after midterm. Now it is time to dive
in.
Firstly, let’s talk about pair data type. What is pair? As you can deduce from the name, it must
have 2 data which is saved together. It is a little similar to arrays, as you can also save multiple
information in arrays, BUT difference is that, you are NOT bound to save information of same
data type. In arrays, if array is of type int, then ALL information must be of int type. In pair,
although you can ONLY save 2 information, they can be of ANY data type you want. Now let’s
see how we can use pairs in our codes.
In order to use pairs, we must include <utility> library in the beginning, like this:
1. #include <iostream>
2. #include <utility>
3. using namespace std;
4. int main(){
5. // first way of initialing, using built-in function make_pair
6. pair <char, string> currency2;
7. currency2 = make_pair('G',"Georgian Lari");
8. // second way of initialing, using direct version
9. pair <char, string> currency;
10. currency.first = '$';
11. currency.second = "US Dollar";
12. cout << currency.first << " " << currency.second <<endl;
13. cout << currency2.first << " " << currency2.second <<endl;
14. //third way
15. pair <int, int> pair_int1 (1, 2);
16. cout << "Pair_int1 " << pair_int1.first << " " << pair_int1.second <<endl;
17. }

That is all about pairs, now it is time to move to a bigger data type, which is Vector.
Vectors are the same as dynamic arrays with the ability to resize themselves automatically
when an element is inserted or deleted, with their storage being handled automatically by the
container.
In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes
the array may need to be extended. Removing the last element takes only constant time
because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in
time.
Benefits of using Vector:
 Dynamic Sizing: Vectors automatically resize when you add or remove elements. This
flexibility is one of the main reasons vectors are preferred over traditional arrays in many
situations.
 Efficient Access: Since vector elements are stored in contiguous memory locations, you
can access elements in constant time using their index, similar to arrays.
 Insertion and Deletion: Insertion of elements is typically done at the end of the vector,
where it operates in constant time. However, inserting or deleting elements at the
beginning or middle of the vector takes linear time due to the need to shift elements

1. #include <iostream>
2. #include <vector>
3. using namespace std;
4. int main(){
5. // way 1 to declare vector
6. // vector <dataType> name({ value1, value2, value3 ....});
7. vector <int> numbers1({1,2,3});
8. // way 2
9. // vector <dataType> name(size, value);
10. vector <int> numbers2(3,3);
11. // way 3 copying everything of another vector
12. // vector<dataType> name(other_vec);
13. vector <int> numbers3(numbers2);
14. }

Vector unlike the array has many built-in functions, which help programmers to manage it easier.
Here are some basic functions:

1. Vector_name.size(); // number of elements in the vector


2. Vector_name.capacity(); // number of elements in the vector
3. Vector_name.max_size(); // maximum number of elements vector can hold
4. Vector_name.empty(); // checks if a vector is empty
5. Vector_name.at(index); // returns a reference to the element at position index in vector
6. Vector_name.front(); // returns a reference to the first element in the vector
7. Vector_name.back(); // returns a reference to the last element in the vector
8. Vector_name.push_back(); // adds new elements into a vector from the back
9. Vector_name.pop_back(); // pops or removes elements from a vector from the back
10. Vector_name.insert(); // inserts new element before the specified index
11. Vector_name.clear(); // it is used to remove all the elements from the vector

Now, let’s try to do some exercises in order to understand the usage of new material better.

You might also like