我可以以这种方式将数组转换为 Pointer 并返回指向常量的指针吗?就内存分配而言是正确的吗?
const int* convert_vector_to_pointer(std::vector<std::pair<int, int> >& v)
{
std::vector<std::pair<int, int> >::iterator it = v.begin();
int* a = new int[2*v.size()];
int i = 0;
for(; it != v.end(); ++it)
{
if(i < 2*v.size())
{
a[i] = (*it).first;
a[i + 1] = (*it).first;
i += 2;
}
}
const int* b = const_cast<const int*>(a);
return b;
}