You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 600851d
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: STL/vector.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -314,6 +314,48 @@ We can declare a vector in several ways. Let us see some of them at a glance and
314
314
|**rbegin()** <br> `v.rbegin()`| Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. |
315
315
|**rend()** <br> `v.rend()`| Returns a reverse iterator pointing to the first element in the vector (reverse end). |
316
316
317
+
Let's see their uses through code:-
318
+
319
+
```cpp
320
+
vector < int > v {1, 2, 3, 4, 5};
321
+
322
+
cout << "Using begin() & end(): ";
323
+
for(auto i = v.begin(); i != v.end(); i++) {
324
+
cout << *i << " ";
325
+
}
326
+
cout << endl;
327
+
328
+
cout << "Using rbegin() & rend(): ";
329
+
for(auto i = v.rbegin(); i != v.rend(); i++) {
330
+
cout << *i << " ";
331
+
}
332
+
cout << endl;
333
+
```
334
+
335
+
Output:
336
+
Using begin() & end(): 1 2 3 4 5
337
+
Using rbegin() & rend(): 5 4 3 2 1
338
+
339
+
In this code, `i` is used as an iterator. We have used auto to keep it simple. If we do not want to use auto, we have to declare an iterator to vector like this - `vector < int >::iterator i` and a reverse iterator like this `vector < int >::reverse_iterator j;`. The we can use `i` in the first loop and `j` in the second loop.. Then the code will be like :-
0 commit comments