VU_Undefined
Page |1
vector<int> v; // Declares a vector of integers
cout << [Link](); // Outputs the size of the vector
v.push_back(2); // Adds element '2' at the end
v.push_back(3); // Adds element '3' at the end
// Each push_back has amortized O(1) time complexity
cout << v[0] << endl; // Access element at index 0
Printing all elements:
for (int i = 0; i < [Link](); i++) {
cout << v[i] << " "; // Prints each element of the vector
Removing last element:
v.pop_back(); // Removes the last element
Accessing the last element:
cout << [Link]() << endl; // Prints the last element
1. Declaring a vector with a specific size:
vector<int> v(5); // Declare vector of size 5, initialized with 0s
v.push_back(10); // Adds element 10 at the end
cout << [Link](); // Outputs: 6 (5 initial + 1 added)
for (...) {
cout << v[i] << " "; // Output: 0 0 0 0 0 10
2. Taking input using push_back()
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x); // Dynamically adds input values to vector
}
VU_Undefined
Page |2
3. Initializing with size and default value
vector<int> v(5, 10); // Vector with 5 elements, each initialized to 10 // Output: 10 10 10 10 10
[Link](3, 11); // Change vector to size 3, all elements = 11 // Output: 11 11 11
[Link](2); // Resize to 2 elements // Output: 11 11
[Link](5); // Resize to 5 elements (new values default to 0) // Output: 11 11 0 0 0
begin() → returns pointer to the *first element*
end() → returns pointer *after the last element*
vector<int> v(5); // Declare a vector of size 5
for (int i = 0; i < [Link](); i++) {
cin >> v[i]; // Input values into the vector
Using Iterators:
vector<int> v(5); // Declare vector
vector<int>::iterator it; // Declare an iterator for vector<int>
for (it = [Link](); it != [Link](); it++) {
cout << +it << ";";
vector <int> v(4);
for (auto &x : v) {
cin >> x;
for (auto x : v) {
cout << x << " ";
* vector <int> v {1, 2, 3, 4, 5};
[Link] ([Link]() + 1);
VU_Undefined
Page |3
for (auto &x : v) {
cout << x << "";
* vector <int> v {1, 2, 3, 4, 5, 6, 7};
[Link] ([Link]() + 2, [Link]() + 5);
* vector <int> v {1, 2, 3, 4};
[Link] ([Link]() + 1, 10);
* vector <int> v {1, 2, 3, 4};
vector <int> nw {10, 11};
[Link] ([Link]() + 2, [Link](), [Link]());
* [Link] ([Link]() + 2, [Link](), [Link]());
* [Link] ([Link]() + 2, {7, 8});
* max_element ([Link](), [Link]());
* vector <int> v {1, 2, 3, 4, 2, 1};
int mx = *max_element ([Link](), [Link]());
cout << mx << endl;
* Code Snippet:
vector <int> v {1, 2, 3, 4, 5};
int max_index= max_element([Link](), [Link]()) - [Link]();
cout << max_index << endl;
*cout<<count([Link](),[Link](),2);
*sort([Link](),[Link]());
*[Link](unique([Link](),[Link]()),[Link]());
ios::sync_with_stdio(false);
[Link](0);
VU_Undefined
Page |4
1. length() / size()
- Returns the number of characters in the string.
Example: [Link](); or [Link]();
2. empty()
- Returns true if the string is empty.
Example: [Link]();
3. at(index)
- Returns the character at the given index (with bounds checking).
Example: [Link](2);
4. front() / back()
- front(): Returns the first character.
- back(): Returns the last character.
Example: [Link](); [Link]();
5. append(str) / +=
- Adds another string at the end.
Example: [Link]("abc"); or s += "abc";
6. substr(pos, len)
- Returns a substring starting from index `pos` with length `len`.
Example: [Link](2, 3);
7. find(str)
- Finds the first occurrence of `str` and returns its index.
- Returns string::npos if not found.
Example: [Link]("abc");
VU_Undefined
Page |5
8. rfind(str)
- Finds the last occurrence of `str`.
Example: [Link]("abc");
9. replace(pos, len, str)
- Replaces part of the string starting at `pos` for `len` characters with `str`.
Example: [Link](2, 3, "xyz");
10. insert(pos, str)
- Inserts `str` into the string at the given position.
Example: [Link](2, "hi");
11. erase(pos, len)
- Removes `len` characters starting from position `pos`.
Example: [Link](2, 4);
12. compare(str)
- Compares two strings:
→ returns 0 if equal,
→ <0 if first string is smaller,
→ >0 if greater.
Example: [Link](s2);
13. c_str()
- Converts a C++ string to a C-style (null-terminated) string.
Example: s.c_str();
14. push_back(char) / pop_back()
- push_back(): Adds a character at the end.
- pop_back(): Removes the last character.
Example: s.push_back('a'); s.pop_back();
15. swap(str2)
- Swaps the contents with another string.
Example: [Link](s2);