是否可以将可变长度数组作为参数传递C++?
Is it possible to pass a variable length array as a parameter in C++?
我以前不知道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 是一个更好的选择。 你会发现在你的代码中传递向量是有效的。
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将__device__ lambda 作为参数传递给 __global__ 函数