如何循环一个 const char**?
How to Loop Through a const char**?
我有一个名为 glfwNames
的 const char**
,它包含所需 GLFW 库扩展的字符串数组的 C 版本。是否可以遍历 const char*(字符串)或由 '[=14=]'
分隔的字符串的各个字符?
const char** glfwNames = glfwGetRequiredInstanceExtensions(&glfwCount)
for (const char** name = glfwNames; *name; ++name)
{
slog("GLFW Extensions to use: %s", *name);
}
这是我从其中一个答案中尝试得出的结果,return
的值
glfwGetRequiredInstanceExtensions
是扩展名数组,GLFW需要http://www.glfw.org/docs/latest/group__vulkan.html#ga1abcbe61033958f22f63ef82008874b1
如果 glfwNames
是 nullptr
终止的:
#include <cstdio>
int main()
{
char const *glfwNames[] = { "foo", "bar", "baz", nullptr };
for (char const **p = glfwNames; *p; ++p)
std::puts(*p);
}
如果您*知道*字符串的数量:
std::uint32_t glfwCount;
const char** glfwNames = glfwGetRequiredInstanceExtensions(&glfwCount)
for (std::uint32_t i{}; i < glfwCount; ++i)
{
slog("GLFW Extensions to use: %s", glfwNames[i]);
}
还要遍历单个 char
s:
for (std::uint32_t i{}; i < glfwCount; ++i)
{
for(char const *p{ glfwNames[i] }; *p; ++p)
std::putchar(*p);
}
我用来遍历 main
参数的常见模式是通过 std::for_each
:
#include <algorithm>
int main(int argc, char* argv[]) {
std::for_each( argv + 1, argv + argc, handler );
}
其中 handler
是采用 const char*
、const std::string&
或 std::string_view
(我使用后者)的任何函数。
类似的方法是否适用于您的问题?请注意,此方法要求您知道字符串数组的长度。
作为旁注,重要的是要知道 std::for_each
的 return 参数是所提供的函数(在本例中为 handler
)。这使得建议的模式能够在已知输入已用尽后进行最后一次调用:
#include <algorithm>
int main(int argc, char* argv[]) {
std::for_each( argv + 1, argv + argc, handler )("Argument To Last Call");
}
这可用于实现在最后接收终止触发器的状态机。
我有一个名为 glfwNames
的 const char**
,它包含所需 GLFW 库扩展的字符串数组的 C 版本。是否可以遍历 const char*(字符串)或由 '[=14=]'
分隔的字符串的各个字符?
const char** glfwNames = glfwGetRequiredInstanceExtensions(&glfwCount)
for (const char** name = glfwNames; *name; ++name)
{
slog("GLFW Extensions to use: %s", *name);
}
这是我从其中一个答案中尝试得出的结果,return
的值glfwGetRequiredInstanceExtensions
是扩展名数组,GLFW需要http://www.glfw.org/docs/latest/group__vulkan.html#ga1abcbe61033958f22f63ef82008874b1
如果 glfwNames
是 nullptr
终止的:
#include <cstdio>
int main()
{
char const *glfwNames[] = { "foo", "bar", "baz", nullptr };
for (char const **p = glfwNames; *p; ++p)
std::puts(*p);
}
如果您*知道*字符串的数量:
std::uint32_t glfwCount;
const char** glfwNames = glfwGetRequiredInstanceExtensions(&glfwCount)
for (std::uint32_t i{}; i < glfwCount; ++i)
{
slog("GLFW Extensions to use: %s", glfwNames[i]);
}
还要遍历单个 char
s:
for (std::uint32_t i{}; i < glfwCount; ++i)
{
for(char const *p{ glfwNames[i] }; *p; ++p)
std::putchar(*p);
}
我用来遍历 main
参数的常见模式是通过 std::for_each
:
#include <algorithm>
int main(int argc, char* argv[]) {
std::for_each( argv + 1, argv + argc, handler );
}
其中 handler
是采用 const char*
、const std::string&
或 std::string_view
(我使用后者)的任何函数。
类似的方法是否适用于您的问题?请注意,此方法要求您知道字符串数组的长度。
作为旁注,重要的是要知道 std::for_each
的 return 参数是所提供的函数(在本例中为 handler
)。这使得建议的模式能够在已知输入已用尽后进行最后一次调用:
#include <algorithm>
int main(int argc, char* argv[]) {
std::for_each( argv + 1, argv + argc, handler )("Argument To Last Call");
}
这可用于实现在最后接收终止触发器的状态机。