我正在尝试练习一些 C++ 的东西,但遇到了一些很难用谷歌搜索或找到答案的东西。
基本上说我们有:
char* myarray[] = {"string","hello", "cat"};
我怎么会说得到 myarray[1] 这是“字符串”,然后遍历字母 s t r i n g。
我正在查看 vector ,想知道是否这是要采用的路径,或者可能采用 myarray[1] 并将其存储到另一个数组中,然后再遍历它。这样做的最佳方法是什么
最佳答案
这在 C++11 中非常容易(使用 std::string
而不是指向字符数组的指针):
#include <iostream>
#include <string>
int main()
{
std::string myarray[] = {"string","hello", "cat"};
for (auto c : myarray[0]) { std::cout << c << " "; }
}
输出(live example):
s t r i n g
关于c++ - 从字符串数组中获取字符值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15418038/