c++ - 从字符串数组中获取字符值

标签 c++ arrays string

我正在尝试练习一些 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/

相关文章:

c++ - Linux C++ : cannot run "nmcli dev list iface" when not logged in

arrays - 读取文本文件时如何忽略标题

java - 如何从文本文件加载文本并与另一个文本文件进行比较?

c++ - 体系结构 x86_64 : error of compilation 的 undefined symbol

c# - 将 C++ Builder 代码转换为 C# .NET(TComponent、TOjbect、TList 等)

c# - 仅包含接口(interface)的 WinRT 组件 DLL?

混淆两个不同字符数组的输出

c++ - 默认堆栈大小

c# - 替换由数字组成的子字符串

JavaScript 正则表达式查找实例