# Vector

```cpp

#include <vector>                                  // include vector  in code

vector<int> arr;                                   // create an empty vector
vector<int> arr(10);                               // create a vector of size 10
vector<int> arr{1,2,3};                            // create vector with values 1,2,3
vector<int> arr(10, 5);                            // create an arr of size 10, all elements initialised to 5    

int n = arr.size();                                // get number of elements in arr 

arr.resize(20, 0);                                 // resize the vector to new final size = 20, each new item set as 0
                                                   // here existing values stay intact as new_size > old_size

// check if vector is empty  
if (arr.empty()){
    cout << "arr is empty";
}
         
arr.push_back(3);                                  // add element at end of vector 
  
arr.insert(arr.begin(), 7);                        // add an element at beginning of vector

arr.insert(arr.end(), brr.begin(), brr.end());     // add another vector at end of a vector  

reverse(arr.begin(), arr.end());                   // reverse vector

arr.clear();                                       // clear all enteries                 

sort(arr.begin(), arr.end());                      // sort vector elements

// sort vector elements in descending order using a comparator function
sort(arr.begin(), arr.end(), [](int lhs, int rhs){
    return (lhs > rhs);     // read it like: lhs should come before rhs if (lhs > rhs)
                            // or read it like: 
                            // In the final sorted array, relation between lhs and rhs is lhs > rhs
});



```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.gomchik.com/tech/c++/vector-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
