In this post, you’ll get answers to these questions:
- What are the Vectors in C++?
- How can I use begin() iterator method?
- How can I use end() iterator method?
- Can I get the last member of a vector by using end() method?
- How can I access to the first element and the last place holder element of a vector?
- Can I print out members of vectors by using begin() and end() methods?
- Can I count members of vectors without using begin() and end() methods?
Learning how to use vector’s Begin() and End() Iterator methods will help you build C++ applications using a C++ Sofware.
Arrays and structs in C++ are explained well in our previous posts. These were coming from the C language but in this post, we will explain another great feature of C++, The Vectors.
Vectors are dynamic arrays included in the <vector> library in modern C++. They can resize themselves automatically when a member of a vector is inserted or deleted. Vectors are the same as dynamic arrays and these dynamic arrays of vectors are handled automatically by the container. Vectors are the way of Modern C++; their members are placed in the contiguous memory storage and can be resized. Vectors can be accessed and traversed using iterators.
When we Insert data into vectors, this may take more time than static arrays because of the need of extending the vector array. Vectors have low memory usage as in dynamic array implementations, because of having good data cache utilization and locality of reference. We can easily access an element of a vector by giving its index between ‘[‘ and ‘]’ as same as arrays, which means vector members can be referenced by indices.
Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices). Linked lists and sets, on the other hand, do not support random access or pointer arithmetic. Vectors are very useful for storing data in lists whose number of elements (length in total) may not be known before setting up the list. Because the vector data structure allocates the necessary memory needed for specific data storage Erasing and clearing vector elements from a vector does not need to free any of the memory associated with that element. That makes vectors much more safe and modern in C++ than arrays.
A vector can be defined in this syntax,
1 2 3 |
std::vector<object type> variable_name; |
Now let’s see how we can use iterator methods of vectors.
Table of Contents
Iterator Methods of Vectors
Iterator (<iterator>) is an object that points an element in a range of elements (i.e. characters of a wide string). We can use Iterators to iterate through the elements of this range using a set of operators, for example using the ++, –, * operators.
- The begin() method returns an iterator pointing to the first element in the vector.
- The end() method returns an iterator pointing to the theoretical element that follows the last element in the vector.
- The rbegin() method returns a reverse iterator pointing to the last element in the vector. It moves from last to first element.
- The rend() method returns a reverse iterator pointing to the theoretical element preceding the first element in the vector.
- The cbegin() method returns a constant iterator pointing to the first element in the vector.
- The cend() method returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
- The crbegin() method returns a constant reverse iterator pointing to the last element in the vector. It moves from last to first element.
- The crend() method returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector.
How do I access the first member of a vector with begin() iterator method?
How do I use the begin() Iterator method?
begin() Iterator Method of std::vector returns an iterator pointing to the first element in the vector. If this pointed vector
is empty then the returned iterator will be equal to end()
Syntax of begin() Iterator Method of std::vector:
1 2 3 |
iterator begin() noexcept; |
Simple begin() example for a vector:
We can access to the value at the pointed address of Iterator with * pointer operator as below,
1 2 3 4 |
std::vector<float> vec{ 1.5, 2.8, 3.6}; float first_value = *vec.begin(); |
here first_value will be equal to the value at the pointed address which is 1.5 in this example;
How do I access the last place holder member of a Vector with the C++ end() iterator method?
How to use the end() Iterator Method
end() Iterator Method of std::vector method returns an iterator pointing to the theoretical element that follows the last element in the vector. This end() Iterator Method acts as a placeholder; attempting to access it results in undefined behavior. In other terms, this method returns the iterator to the element following the last element
Syntax of the end() Iterator Method of std::vector:
1 2 3 |
iterator end() noexcept; |
Simple end() example for a vector:
We can not access to the last member of a vector by using end() method with * pointer operator. We can only access to the value at the pointed address of Iterator as below,
1 2 3 4 |
std::vector<float> vec{ 1.5, 2.8, 3.6}; float last_value = *vec.end(); |
in this example user can not get the last member. Because this end() method returns the iterator to the element following the last element. That means it returns the value at the pointed address which means it can be anything written in that address. If you really want to reach to the last element value you must use back() method without using * pointer operator as below;
1 2 3 4 |
std::vector<float> vec{ 1.5, 2.8, 3.6}; float last_value = vec.back(); |
Is there an example of the C++ begin() and end() iterator methods?
Here are two different iterator methods to print out members of vectors. In the second use we show how to use begin() and end() iterators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> int main() { std::vector<float> vec{ 1.5, 2.8, 3.6};; // Vector in floats for (auto v: vec) std::cout << v << ','; std::cout << std::endl; std::vector<float>::iterator vi; for (vi= vec.begin(); vi!= vec.end(); vi++) std::cout << *vi << ','; std::cout << std::endl; getchar(); return 0; } |
How to access the first and last members of a C++ Vector?
Use the front() and back() C++ vector iterator methods.
If you really want to reach to the first or last element use front() and back() methods. Note that in the std::vector, the begin() and the end() methods are Iterators while the front() and the back() methods are elements of vectors. as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> int main() { std::vector<float> vec{ 1.5, 2.8, 3.6};; // Vector in floats float front_value = vec.front(); float back_value = vec.back(); std::cout << "Front Value:" << front_value << std::endl; std::cout << "Back Value:" << back_value << std::endl; getchar(); return 0; } |
In this example you can get the value of first element but you can not get the value of last element, because the end() method returns the iterator to the element following the last element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> int main() { std::vector<float> vec{ 1.5, 2.8, 3.6};; // Vector in floats float first_value = *vec.begin(); float last_value = *vec.end(); std::cout << "First Value:" << first_value << std::endl; std::cout << "Last Value:" << last_value << std::endl; getchar(); return 0; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition