假设我需要这样做
我有一个函数,比如:
int dbs_access(chat* name, char* sql, void* outbuf, unsigned int buflen, int* outresutl)
{ do some query here and output result}
我想从DBS获取一些数据,所以我需要获取一些缓冲区并调用上面的函数,因为我不知道表中有多少条记录,所以我不能使用数组,我不想使用 new 或 malloc 来获取一些内存,因为释放内存在这里可能是一个问题。因此我想使用 vector 。但我不确定 c++ 标准是否能保证这一点,任何观点?
unsigned int count;
dbs_access(...,"select count(*) from..",&count,sizeof(count),...)
std::vector records[count];
assert(records.size()==count)
dbs_access(...,"select from..",&records[0],records.size(),...)
最佳答案
records.data() 会给你指针,是的,在这里使用 vector 是可以接受的,尽管你可能想像这样初始化大小:
std::vector records(count);
(而不是创建 vector 数组)
关于c++ - 获取 vector 中元素的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156426/