-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.cpp
28 lines (28 loc) · 1 KB
/
join.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
void join(const std::vector<int> &from, std::vector<int> &to)
{
std::vector<int> temp (to);
to.resize(from.size()+temp.size(), 0); //complement vector with zeros
std::vector<int>::const_iterator it_from = from.begin();
std::vector<int>::iterator it_temp = temp.begin(), it_to = to.begin();
while (it_from != from.end() && it_temp != temp.end()) //until the end of one of vectors
{
if (*it_from < *it_temp)
{
*it_to = *it_from;
++it_from, ++it_to;
}
else
{
*it_to = *it_temp;
++it_temp, ++it_to;
}
}
if (it_from == from.end()) //this part copies the rest of the last vector
{
std::copy(it_temp, temp.end(), it_to);
}
else
{
std::copy(it_from, from.end(), it_to);
}
}