gpt4 book ai didi

c++ - 如何获取指向 2D std::vector 内容的指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:39:32 28 4
gpt4 key购买 nike

给定

std::vector<char> theVec

我可以这样获取指针

char* cc = &theVec[0]

如果 vector 声明如下:

std::vector<std::vector<char> > theVec

如何将指向 theVec 头部的指针转换为 char*

char cc* = 

最佳答案

How do you get a pointer to the head of theVec [as a char*]?

你不能。 vector 的头部类型为 std::vector<char> , 不是 char , 和一个 std::vector<std::vector<char> >不将其数据存储在一个连续的 block 中。

相反,您可以这样做:

std::vector<char> theVec;
theVec.resize(xSize*ySize);
char cc* = &theVec[0];
char tmp = theVec[x*xSize + y];//instead of theVec[x][y]

关于c++ - 如何获取指向 2D std::vector 内容的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6734472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com