Vectors and pairs are two important data structures in the C++ programming language. They are used to store and manipulate collections of data in a convenient and efficient way. In this article, we will explore the basics of vectors and pairs, how they are used in C++, and some common operations that can be performed on them.
A vector is a dynamic array that can grow or shrink as needed. It is a container class in the C++ Standard Template Library (STL) that stores a collection of elements. Vectors are similar to arrays, but they have some advantages over arrays, such as being able to resize dynamically, and providing easy-to-use methods for inserting and deleting elements.
A pair is a simple class that stores two elements of different or same data types. Pairs are also a container class in the STL, and are typically used to store a collection of related data. For example, a pair might be used to store a name and an age, or a latitude and longitude.
Creating a Vector
Creating a vector in C++ is simple. The syntax for creating a vector is as follows:
vector<type> name;
For example, to create a vector of integers, you would use the following code:
vector<int> numbers;
This creates an empty vector of integers, which can be used to store a collection of integers.
Inserting Elements into a Vector
Once a vector has been created, elements can be inserted into it using the push_back() method. The push_back() method adds an element to the end of the vector. For example, to insert the number 5 into the vector of integers created above, the following code would be used:
numbers.push_back(5);
The vector now contains the single element 5.
Removing Elements from a Vector
Elements can be removed from a vector using the pop_back() method. The pop_back() method removes the last element from the vector. For example, to remove the number 5 from the vector of integers created above, the following code would be used:
numbers.pop_back();
The vector is now empty.
Accessing Elements in a Vector
Elements in a vector can be accessed using the [] operator, just like an array. For example, to access the first element in the vector of integers created above, the following code would be used:
cout << numbers[0] << endl;
This would output the value 5, as that is the only element in the vector.
Creating a Pair
Creating a pair in C++ is also simple. The syntax for creating a pair is as follows:
pair<type1, type2> name;
For example, to create a pair of a string and an integer, you would use the following code:
pair<string, int> person;
This creates a pair with a string as the first element and an integer as the second element. The pair can be used to store a name and an age, for example.
Accessing Elements in a Pair
Elements in a pair can be accessed using the .first and .second members. For example, to access the first element in the pair of a string and an integer created above, the following code would be used:
cout << person.first << endl;
This would output the string stored in the first element. To access the second element, the .second member would be used:
cout << person.second << endl;
This would output the integer
