Here it is a quick one, that just wrote:
void ReverseByIndices(std::vector<CString> &items, std::vector<int> positions)
{
std::vector<CString>::iterator it;
std::vector<int>::iterator itPos;
std::vector<CString> indices;
itPos = positions.begin();
while (itPos != positions.end())
{
indices.push_back(items[*itPos]);
itPos++;
}
std::reverse(indices.begin(), indices.end());
for (int i = 0; i < indices.size(); i++)
{
it = std::find(items.begin(), items.end(), indices[i]);
if (it != items.end()) items.erase(it);
}
for (int i = 0; i < indices.size(); i++)
{
items.insert(items.begin() + positions[i], indices[i]);
}
indices.clear();
positions.clear();
}
And to test the above function it returns the same output using Lee's list's samples:
std::vector<CString>::iterator it;
std::vector<CString> items;
std::vector<int> positions;
items.push_back("A");
items.push_back("B");
items.push_back("C");
items.push_back("D");
items.push_back("E");
items.push_back("F");
items.push_back("G");
items.push_back("H");
//positions.push_back(1);
//positions.push_back(3);
//positions.push_back(4);
positions.push_back(0);
positions.push_back(2);
positions.push_back(5);
positions.push_back(6);
acutPrintf(_T("\nOriginal: "));
for (int i = 0; i < items.size(); i++)
{
acutPrintf(_T("%s "), items[i]);
}
ReverseByIndices(items, positions);
acutPrintf(_T("\nAfter: "));
for (int i = 0; i < items.size(); i++)
{
acutPrintf(_T("%s "), items[i]);
}
items.clear();