Skip to content

Commit 5246a90

Browse files
vector_pop_front.cpp
1 parent 44fc3eb commit 5246a90

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

vector_pop_front.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <cassert> //assert
5+
6+
//https://stackoverflow.com/questions/9445327/fast-way-to-implement-pop-front-to-a-stdvector
7+
8+
template<typename T>
9+
void pop_front(std::vector<T>& vec)
10+
{
11+
assert(!vec.empty());
12+
vec.erase(vec.begin());
13+
}
14+
15+
int main() {
16+
std::vector<int> vec = {1,2,3,4,5,6};
17+
pop_front(vec);
18+
19+
for(int e : vec){
20+
std::cout << e << " ";
21+
}
22+
std::cout << std::endl;
23+
24+
return 0;
25+
}
26+
27+
//2 3 4 5 6

0 commit comments

Comments
 (0)