Created
May 6, 2020 13:11
-
-
Save yasukei/0b7c631c089035f0745f40932424e854 to your computer and use it in GitHub Desktop.
std::forward_list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <forward_list> | |
int main() | |
{ | |
std::forward_list<int> l; | |
auto print = [&]() { | |
std::cout << "l.empty(): " << l.empty() << "\n"; | |
std::cout << "l.max_size(): " << l.max_size() << "\n"; | |
for (auto it = l.begin(); it != l.end(); ++it) | |
{ | |
std::cout << *it << "\n"; | |
} | |
}; | |
std::cout << "\n[Initial state]\n"; | |
print(); | |
std::cout << "\n[After push_front()]\n"; | |
l.push_front(1); | |
print(); | |
std::cout << "\n[After clear()]\n"; | |
l.clear(); | |
print(); | |
std::cout << "\n[After insert()]\n"; | |
l.push_front(0); | |
for (int i = 1; i < 10; i++) | |
{ | |
l.insert_after(l.begin(), i); | |
} | |
print(); | |
std::cout << "\n[After erase()]\n"; | |
for (auto it = l.begin(); it != l.end(); ) | |
{ | |
if (*it % 2 == 0) | |
{ | |
it = l.erase_after(it); | |
} | |
else | |
{ | |
++it; | |
} | |
} | |
print(); | |
std::cout << "\n[After resize()]\n"; | |
l.resize(10); | |
print(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Initial state] | |
l.empty(): 1 | |
l.max_size(): 1152921504606846975 | |
[After push_front()] | |
l.empty(): 0 | |
l.max_size(): 1152921504606846975 | |
1 | |
[After clear()] | |
l.empty(): 1 | |
l.max_size(): 1152921504606846975 | |
[After insert()] | |
l.empty(): 0 | |
l.max_size(): 1152921504606846975 | |
0 | |
9 | |
8 | |
7 | |
6 | |
5 | |
4 | |
3 | |
2 | |
1 | |
[After erase()] | |
l.empty(): 0 | |
l.max_size(): 1152921504606846975 | |
0 | |
8 | |
6 | |
4 | |
2 | |
[After resize()] | |
l.empty(): 0 | |
l.max_size(): 1152921504606846975 | |
0 | |
8 | |
6 | |
4 | |
2 | |
0 | |
0 | |
0 | |
0 | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment