Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

_torrent_'s blog

By _torrent_, history, 4 hours ago, In English

Let me take you on a very important journey and learn the difference between two very important things in programming, especially when you come to learn "C++" Together, we will differentiate between two very important needs: Vector, Array First, in terms of the size of the matrix: Array Its size is fixed, meaning when you create a matrix of a certain size, it will not be possible to make it bigger or smaller anymore Vector Its size is variable, meaning you can supply items and remove items according to what you need Example

int arr[5]; vectorv; The function through which you can add elements to the vector push.back(item); Here you can add the element to the end of the array

The function through which you can delete vector elements pop.back(item); Here you can delete the element from the end of the array

Secondly, in terms of memory management:

Array You must manage the memory yourself, and if you need to change the size, you will need to create a new array Vector It manages the memory itself, and expands itself automatically when you add new items.

Third, in terms of built-in functions: Array It does not provide you with functions such as adding and removing Vector It has many built-in functions, such as push_back for adding and erase for removing, as we said above

Fourth, in terms of software security: Array It could be dangerous if you try to access a memory location that is not allocated

Vector More secure because it checks limits in some operations.

Example int arr[5];

arr[10] = 50; This is wrong and may crash the program.

std::vector vec(5); vec.at(10) = 50; This will throw an exception if you try to reach a place that does not exist.

Function at.(index of item); Like V[index of item ]; Where V is the name of the array

In short, vector is more flexible, secure, and easy to use compared to a regular matrix

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it