-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveEveryOtherItem.cpp
executable file
·77 lines (60 loc) · 1.42 KB
/
RemoveEveryOtherItem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <list>
#include <vector>
#include <iostream>
#include <ctime>
using namespace std;
template <typename Container>
void removeEveryOtherItem( Container & lst )
{
auto itr = lst.begin( );
while( itr != lst.end( ) )
{
itr = lst.erase( itr );
if( itr != lst.end( ) )
++itr;
}
}
template <typename Container>
void print( const Container & c, ostream & out = cout )
{
if( c.empty( ) )
out << "(empty)";
else
{
auto itr = begin( c );
out << "[ " << *itr++; // Print first item
while( itr != end( c ) )
out << ", " << *itr++;
out << " ]" << endl;
}
}
int main( )
{
list<int> lst;
for( int i = 0; i < 9; ++i )
lst.push_back( i );
removeEveryOtherItem( lst );
print( lst, cout );
/*
clock_t start, end;
for( int N = 100001; N <= 5000000; N *= 2 )
{
list<int> lst;
vector<int> vec;
for( int i = 0; i < N; ++i )
{
lst.push_back( i );
vec.push_back( i );
}
start = clock( );
removeEveryOtherItem( lst );
end = clock( );
cout << "list " << N << " " << double(end-start)/CLOCKS_PER_SEC << endl;
start = clock( );
removeEveryOtherItem( vec );
end = clock( );
cout << "vector " << N << " " << double(end-start)/CLOCKS_PER_SEC << endl;
}
*/
return 0;
}