gpt4 book ai didi

c++ - 确定字符串是否具有相同的字符

转载 作者:行者123 更新时间:2023-11-28 05:27:25 25 4
gpt4 key购买 nike

是否有像 find_first_not_of 这样的函数返回 true 或 false 而不是位置?我不需要位置,而是字符串是否包含所有相同的字符。

最佳答案

您可以编写自己的函数:

bool all_chars_same(string testStr) {
char letter = testStr[0];

for (int i = 1; i < testStr.length(); i++) {
if (testStr[i] != letter)
return false;
}

return true;
}

或者使用内置的find_first_not_of:

bool all_chars_same(string testStr) {
return testStr.find_first_not_of(testStr[0]) == string::npos;
}

关于c++ - 确定字符串是否具有相同的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com