vectors in c++

what is the syntax to use vectors in codechef ?

1 Like

to create a vector we write
vector name of vector

for example
vector arr;
vectorarr;

to push element in a vector we write

arr.push_back(data);

Vectors are an interesting commodity in C++ and pretty useful to learn as you can implement optimized code without having to write hundred’s of line,

Some useful commands, with the help of a program

#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> numbers;                   //Creating a vector, name: numbers, type: int, can change accordingly
numbers.push_back(2);                  //numbers contains {2}
numbers.push_back(1);                  //numbers contains{2,1}
sort(numbers.begin(), numbers.end());  //Sorts the vector now the order is {1,2}
int a=numbers.at(0);                   //Simmilar to accessing an array, use .at(), value of a becomes 1 
}

Theese are some of the most important commands used most of the time!
Practice creating other types of vector, such as long and double eg. vector numbers and practice.
Finally, don’t worry about the time complexity of the sort function, it is one of the best and stable sorting algorithms already implemented and ready for use.

For more reference and advanced topics you can watch videos by @rachitiitr on STL on Youtube, here.