SetAtGrow issue in vector and array

hi

setatgrow in vector is giving issue as i have t orepalce carray to vector i want setatgrow logic in vector:

i did like the below:

for CArray in mfc:

CArray<MtPoint2d, MtPoint2d> m_Points; // Array of points
std::vector<MtPoint2d> m_PointVectors; // Array of vectors

m_Points.Add(1,2);
m_Points.Add(3,4)
m_Points.Add(5,0);
m_Points.Add(0,6);

m_PointVectors.emplace_back(1,2);
m_PointVectors.emplace_back(3,4);
m_PointVectors.emplace_back(5,0);
m_PointVectors.emplace_back(0,6);



m_Points.SetAtGrow(2, MtPoint2d(7, 8));
m_Points.SetAtGrow(7, MtPoint2d(7, 8));

i can directly do this in array ( as the SetAtGrow API is there)

but in vector how to do ?

i did tw o things but its giving problerm:


m_PointVectors.resize(ptNum + 1);
m_PointVectors[ptNum] = point;

//but its giving problem
( as in the above scenario i.e m_Points.SetAtGrow(2, MtPoint2d(7, 8));)
if i do the above code the value will be replace at the 2 position but 3rd position will be repalced with 0,0
instead of 6,0 ) its happening in array but not with this logic

if i do this alone
m_PointVectors[ptNum] = point;
its crash in m_Points.SetAtGrow(7, MtPoint2d(7, 8));


please help me how to setatgrow in vector
You don't want to resize the vector if the index is smaller than the size.
1
2
3
4
5
if (ptNum >= m_PointVectors.size())
{
	m_PointVectors.resize(ptNum + 1);
}
m_PointVectors[ptNum] = point;
Topic archived. No new replies allowed.