是否可以将可变长度数组作为参数传递C++?

Is it possible to pass a variable length array as a parameter in C++?

本文关键字:参数传递 C++ 数组 是否      更新时间:2023-10-16

我以前不知道V的值。它是在我在程序中打开的文件中找到的。它不能被定义为这样的#DEFINE V __。它不能作为全局变量工作。输入文件根据内容更改 V。期望参数通过并使用在极客的极客中找到的 djisktra 算法。

我尝试全局声明 V,但出现错误说"变量必须具有常量值"。

void dijkstra(int graph[V][V], int src, int V)
//array function being pasted, error is the V in graph[V]
//V is defined at beginning of main as 
int V;
//where V is changed
while(std::getline(file2,newstr))
{
if(newstr.find(check) != std::string::npos)
{
V++;
}
}
//where it is passed in main
for(int i = 0; i < V; i++)
{
size = V;
dijkstra(array[size][size], i, V);        
}

不要使用 C 样式数组。使用标准库中的std::vector和朋友,如果您想知道,可以询问尺寸。

转换:

void dijkstra(const std::vector<std::vector<int>>& graph, int src) {
auto v = graph.size();
// ... Other code.
}

对于插入,您可以使用push_back

std::vector<std::vector<int>> graph;
while(std::getline(file2,newstr)) {
if(newstr.find(check) != std::string::npos) {
std::vector<int> row;
row.push_back(...);
graph.push_back(row);
}
}

然后像常规变量一样传入它:

dijkstra(graph, src);

如果所有这些矢量的东西看起来真的很丑,typedef它看起来更友好。

对于 c 样式的数组,您需要在编译时知道大小。像int N;这样的变量是运行时值。像constexpr int N = 9;这样的变量在编译时可用,不能改变。

如果你需要一个在运行时相当大的数组,你需要某种动态数组。最常见的是std::vector.

void dijkstra(std::vector<int> graph, int src, int V)
std::vector<int> graph;
graph.resize(V * V); // vector are resizable
for(int i = 0; i < V; i++)
{
size = V;
dijkstra(graph, i, V);        
}
是否可以将

可变长度数组作为参数传递C++。

不。

标准C++不支持可变长度数组,但请继续阅读,它们有一个令人惊讶的更好的替代方案。


我打开的文件中找到 V 之前,我不知道它的值 在程序中。

创建一维向量很简单,在代码找到 V,不需要编译时常量。

在我的一个程序启动的早期,gBoard向量是使用argv[3]和argv[4]构建的。 下面是一个片段:

aTermPFN               += argv[1];    // ouput tty, /dev/pts/<argv[1]>
fillPatternChoiceLetter = argv[2][0];
aMaxRow                 = stoi(argv[3]);
aMaxCol                 = stoi(argv[4]);
userDim                 = true;

显然,该计划已经开始...V大小很容易从(aMaxRow * aMaxCol)计算出来。

我发现很容易按行大顺序访问 1d 向量(或 1d 数组),就好像它是一个 2d 矩阵一样,具有以下功能:

// game-board-index: computes index into the single dimension vector
//                   from 2d (row, col) matrix coordinates
size_t gbIndx(int r, int c) { return static_cast<size_t>((r * maxCol) + c); }
// a 2d game board of cells
// 2d access (row major order) implemented using 1d access
Cell_t*  getCell( int r, int c )   { return (gBoard [gbIndx(r,c)]); } 
// 1d access is surprisingly convenient for many functions
Cell_t*  getCell( uint gbIndex  )  { return (gBoard [gbIndex]);     } 

初始化用法示例:

//              vvvvvvvvvvvvvvvvvvv_-- 2d matrix access
gBoard [ gbIndx((midRow+1), midCol)   ] -> setOptionX();
//       ^^^^^^--1d row-major order index 

随机的gGoard在1d中是微不足道的:

void GOLUtil_t::setRandom() { CellVec_t myVec(gBoard);复制细胞载体

random_device路; mt19937_64 gen(rd()); shuffle (myVec.begin(), myVec.end(), gen);随机播放顺序

整数计数 = 1; for ( auto it : myVec )//随机标记一半的单元格 { if(count++ & 1) it->setAlive();每个奇数单元格 } }


https://en.cppreference.com/w/cpp/container/vector 的说明:

"元素是连续存储的,这意味着元素不仅可以通过迭代器访问,还可以使用偏移量来指向元素的常规指针。这意味着指向向量元素的指针可以传递给任何期望指向数组元素的指针的函数。


我很惊讶 1d 访问启用更简单代码的频率。

for (auto it : gBoard)
it->init();        // command each cell to init

总结:

尽管 std C++ 不支持可变长度数组 (vla),但我相信您会发现 std::vector 是一个更好的选择。 你会发现在你的代码中传递向量是有效的。