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
|
#include <iostream>
#include <vector>
int main()
{
using USHORT = unsigned short;
// create a 2D vector for testing purposes
std::vector<std::vector<USHORT>> vec = { { 0, 4, 8, 9, 5, 11, 10, 2, 12, 7, 3, 6, 1, 0, 0 },
{ 0, 10, 11, 9, 8, 5, 4, 1, 7, 3, 6, 12, 1, 0, 0, },
{ 0, 0, 8, 9, 5, 4, 11, 10, 2, 12, 7, 3, 6, 1, 0 } };
// lets display the vector
for (USHORT i = 0; i < vec.size(); i++)
{
for (USHORT j = 0; j < vec[i].size(); j++)
{
std::cout << vec[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
for (USHORT j = 0; j < vec.size(); j++)
{
bool not_done = false;
// walk through the array and erase any found elements
while (not_done == false)
{
not_done = true;
for (USHORT i = 0; i < vec[j].size(); i++)
{
if (vec[j][i] == 1 || vec[j][i] == 2)
{
vec[j].erase(vec[j].begin() + i);
vec[j].push_back(0);
not_done = false;
break;
}
}
}
}
// use a range-based loop to display the modified array
for (const auto& i : vec)
{
for (const auto& j : i)
{
std::cout << j << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
}
|