adding the iterator exercise code

This commit is contained in:
Jidong Xiao
2024-02-09 13:38:32 -05:00
parent 2c0f825d20
commit cb7ea6977f

View File

@@ -140,6 +140,36 @@ itr++;
assert (*itr == 100); // might seem ok... but rewrite the code to avoid this!
```
### Exercise
What is the output of this program?
#include <list>
#include <iostream>
```cpp
int main(){
std::list<int> lst;
lst.push_back(150);
lst.push_back(250);
lst.push_back(350);
lst.push_back(450);
std::list<int>::iterator itr;
itr = lst.begin();
++itr;
*itr += 5;
std::list<int>::iterator itr2 = lst.begin();
while(itr2 != lst.end()){
std::cout << *itr2 << std::endl;
itr2++;
}
}
```
## 10.3 Working towards our own version of the STL list
- Our discussion of how the STL list&lt;T&gt; is implemented has been intuitive: it is a “chain” of objects.