c++ - 如何遍历字符串中的每个字符?

标签 c++ string char

我有一个字符串,我需要在其中获取每个字符并进行一些检查:

std::string key = "test"
int i = 0;
while (key.at(i))
{
    // do some checking
    i++;
}

这里的问题是,索引 i 最终会超出范围,因此系统会崩溃。我该如何解决这个问题?

最佳答案

std::string key = "test"
for(int i = 0; i < key.length(); i++)
{
    //do some checking
}

关于c++ - 如何遍历字符串中的每个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11303391/

相关文章:

c++ - 关于搜索和排序算法的问题

c++ - 从 double vector 中找到多种模式

c++ - 连接预期 token ';' 得到 ')'

arrays - 添加具有多个真实 filterxml 变量的 ifs 函数

c++ - NeoPixel Arrays 让我的 ESP32 重新启动!为什么?

javascript - 编辑String的原型(prototype)

python - Pandas 数据帧 : Cannot convert string into a float

mysql - SQL 子串代码

c++ - C++中的字符赋值

java - 如何解决这个问题,我希望它排列整齐,而不是交错或重复