I have an array of vectors, and I would like to delete an entry (in this case at index 3
of the array), and move all vectors up 1 position to fill the deleted slot.
I currently have two methods that works.
Option 1 Swap each vector: std::swap(m_conditions[i], m_conditions[i+1]);
Option 2 Copy each vector: m_conditions[i] = m_conditions[i + 1];
I feel that I should be able to move
these vectors around without copying and swapping, can anyone suggest a better approach? (Without replacing the m_conditions
array with a vector)
#include <vector>
class clsCondition
{
public:
int conditionValues;
};
int main()
{
int arrayCount = 50;
int deleteEntry = 3;
std::vector<clsCondition> m_conditions[50];
for (int i = deleteEntry; i < arrayCount - 1; i++)
{
//std::swap(m_conditions[i], m_conditions[i+1]); // Option 1 Swap each vector
m_conditions[i] = m_conditions[i + 1]; // Option 2 Copy each vector
}
arrayCount--;
m_conditions[arrayCount].clear();
}
To make option 2 move the vector rather than copying it, change your code as below
m_conditions[i] = std::move(m_conditions[i + 1]);
Your option 1 with the swap will move rather than copy; however, doing the move yourself is probably clearer.