Fixed mistyped words (#25397)
This commit is contained in:
@ -104,13 +104,13 @@ std::string second_item = a[2]; // gets "element"
|
|||||||
|
|
||||||
Looping over elements in a C++ `std::vector` is pretty different from looping over elements in a vector in JavaScript or Ruby. Due to C++ being a thin abstraction of C, you can only loop over elements using these nifty little variables called iterators to access each element.
|
Looping over elements in a C++ `std::vector` is pretty different from looping over elements in a vector in JavaScript or Ruby. Due to C++ being a thin abstraction of C, you can only loop over elements using these nifty little variables called iterators to access each element.
|
||||||
Iterators often come in the form of pointers which are variables that store the memory address of another variable. You can learn more about pointers [here](https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm).
|
Iterators often come in the form of pointers which are variables that store the memory address of another variable. You can learn more about pointers [here](https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm).
|
||||||
However, because iterators act as pointers (or vice-versa), in order to see what they point to, you need to dereference it into a variable of the appropirate type.
|
However, because iterators act as pointers (or vice-versa), in order to see what they point to, you need to dereference it into a variable of the appropriate type.
|
||||||
How do we do this?
|
How do we do this?
|
||||||
HERE. WE. GO!
|
HERE. WE. GO!
|
||||||
```cpp
|
```cpp
|
||||||
std::vector<std::string> a{"test", "element", "access"};
|
std::vector<std::string> a{"test", "element", "access"};
|
||||||
for(auto it = v.begin(); it != v.end(); it++) { //notice use of auto keyword
|
for(auto it = v.begin(); it != v.end(); it++) { //notice use of auto keyword
|
||||||
cout<<*it<<endl; //Will print out string that the iterator is currently ppointing to
|
cout<<*it<<endl; //Will print out string that the iterator is currently pointing to
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
From here, you can do all sorts of cool stuff, like manipulating the vector or mess around with its order as you please!
|
From here, you can do all sorts of cool stuff, like manipulating the vector or mess around with its order as you please!
|
||||||
@ -234,4 +234,4 @@ int main(){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also sort in descending using lamda like the one above.
|
You can also sort in descending using lambda like the one above.
|
||||||
|
Reference in New Issue
Block a user