PrevUpHomeNext

day-21: c++ STL: std::vector


STL is the c++ Standard Template Library. Most c++ compilers have implemented its own STL. So if you have installed a mainstream c++ compiler, you can use STL directly without additional setup. Microsoft visual c++ (visualcpp), GNU c++ compiler (gcc), clang c++ compiler are all such compilers that have implemented STL.

std::vector

std::vector is the c++ vector container class type.

-) First, It is array (dynamic)

The element of std::vector are continuous in memory. And the memory space is dynamic allocated.

-) Second, it is template

std::vector is class template, you must instantiate it to a specific class. The template parameter of std::vector is the type of its value (its element).

Include

#include <vector>

Template Instantiation

From std::vector to std::vector<int>, or std::vector<float>, etc.

Create vector container object

Create a vector object v1, with size 0

std::vector<int> v1;

Create a vector object v2, with size 7 (not initialized)

std::vector<int> v2(7);

Create a vector object v3, initialize with 7 (it has 1 element)

std::vector<int> v3{7};

Create a vector object v4, initialize with 2,3,4,2,6; (5 elements)

std::vector<int> v4{2, 3, 4, 2, 6};

Method: .size()

Method .size() returns the element count of the vector.

std::vector<float> v1{1.2f, 2.3f, 3.2f, 2.4f, -2.7f, 7.9f};
auto size = v1.size();	// 6

Access std::vector elements

Range-based for loop can be used to access each element of the std::vector object.

#include <iostream>
#include <vector>

int main()
{
	std::vector<float> v1{1.2f, 2.3f, 3.2f, 2.4f, -2.7f, 7.9f};

	// range-based for loop
	// v1 is used as the value list of the range-based for loop.
	for (const float & x: v1)
	{
		std::cout << "element: " << x << std::endl;	
	}
}

For-loop and the method .operator[] of std::vector can be used to access the array elements.

std::vector<float> v1{1.2f, 2.3f, 3.2f, 2.4f, -2.7f, 7.9f};

for (int i=0; i<v1.size(); ++i)
	std::cout << v1[i] << '\n';	// .operator[]

Method .push_back

The method .push_back of std::vector can be used to add element to the end of the std::vector object.

std::vector<float> v1{1.2f, 2.3f, 3.2f, 2.4f, -2.7f, 7.9f};
v1.push_back(3.14f);
v1.push_back(1.43f);
v1.push_back(2.92f);

Method .empty()

Method .empty of std::vector is used to check if the std::vector object is empty.

std::vector<int> v1;
std::vector<float> v2{2.3f, 3.9f};
bool s1 = v1.empty();	// true
bool s2 = v2.empty();	// false

Method .data()

Note that the std::vector elements storage in memory is continuous.

The method .data() of std::vector is used to get the pointer to the first memory space of std::vector object.

#include <vector>
#include <iostream>

int main()
{
	auto vector = std::vector<int>{2, 3, 4, 5, 7, 1, 4};
	int * ptr = vector.data();
	*ptr = 112233;

	std::cout << *ptr << std::endl;	// 112233

	for (const int x: vector)
		std::cout << x << ' ';
	std::cout << std::endl;

	for (int i=0; i<vector.size(); ++i)
	{
		std::cout << *ptr << ' ';
		++ptr;	// Move pointer to next element
	}
	std::cout << std::endl;
}

output:

112233
112233 3 4 5 7 1 4
112233 3 4 5 7 1 4

c++ iterator - std::vector

the c++ iterators

The iterator is a special class of c++ STL library.
Many c++ STL containers (it is named ranges too) have its related iterator,
they share many same or similar principles.

An iterator object stands for an access position of the range.

Method .begin(), .end() of the range

std::vector has two methods: .begin(), .end(), and some other ranges have them too.

// std::vector can be named container, and can be also named range
std::vector<int> v2 = {2, 3, 4, 5, 7, 9, 13};
auto itr1 = v2.begin();	// return an iterator to store the start position.
auto itr2 = v2.end();	// return an iterator to store the position past the last element.

iterator type : the iterator type that the .begin() and .end() returns has some complexity, you can use auto here to ignore those complexity, auto works at many cases. Feel free to use auto, you just only need to know how to use it currently.

Method .operator++, .operator-- of the iterator

The iterator type is a class, it defined many convenient methods.

++itr1;	// Move itr1 to store the next position of the range.
itr1++;	// Move itr1 to store the next position of the range.

--itr1;	// Move itr1 to store the previous position of the range.
itr1--;	// Move itr1 to store the previous position of the range.

Method .operator* of the iterator

An iterator is an object,
it stores the position of related range.
If you want to take the element from the range via the iterator,
you can use method .operator*

std::vector<int> v3{4, 5, 7, 2, 9, 6};	// v3 is a std::vector object, it is a range

auto itr_jj = v3.begin();	// Feel free to use auto

int x = * itr_jj;	// Method .operator*    it takes 4 from the range

++itr_jj;
++itr_jj;
++itr_jj;

int a = * itr_jj;	// Take 2 from the range through the iterator now

Method .operator- of the iterator

The method .operator- is the method of the iterator:

auto distance = itr2 - itr1;

It means calculating the distance of two iterators, the result is an integer type.

#include <vector>
#include <iostream>

int main()
{
	std::vector<int> v4{9, 3, 8, 7, 5, 4, 2};

	auto end = v4.end();

	// const can be applied to the iterator, so it is immutable.
	const auto itr1 = v4.begin();

	auto itr2 = v4.begin();
	++itr2;
	itr2++;
	++itr2;
	itr2 += 2;	// Move to next 2 elements.

	auto dist1 = itr2 - itr1;	// result is 5, because their distance is 5.
	auto dist2 = end - itr1;	// result is 7, because their distance is 7.
	auto dist3 = end - itr2;	// result is 2, because their distance is 2.

	std::cout << dist1 << ',' << dist2 << ',' << dist3 << std::endl;
}







Written on Dec 07, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext