替换字符串C++标点符号
Replace punctuation in C++ string
我试图理解为什么binary_search((找不到某些标点字符,而find((却找到了:
array<char, 25> punctuation_chars{''', '"', ',', '.', ';', ':', '+', '*', '-', '_', '?', '!', '=', '|', '^', '/', '', '(', ')', '[', ']', '{', '}', '<', '>'};
bool is_punctuation(char c)
{
auto ret = find(cbegin(punctuation_chars), cend(punctuation_chars), c) != cend(punctuation_chars);
// auto ret = binary_search(cbegin(punctuation_chars), cend(punctuation_chars), c);
if (c == ',')
cout << c << " is" << (ret ? "" : " not") << " punctuation" << endl;
return ret;
}
注释行不起作用(例如,对于 c == ',' 返回 false(,而查找返回 cend(punctuation_chars(...
punctuation_chars
没有排序,因此std::binary_search
不起作用。您需要致电std::sort
:
std::sort(std::begin(punctuation_chars), std::end(punctuation_chars));
相关文章:
- C++中的标点符号是什么
- 如何通过 scanf() 读取没有标点符号的字符串?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++在标点符号后将字符串换行
- Flex Lexer 模式匹配句子分隔符/标点符号作为 URL 路径部分
- 如何使用输入文件的 ispunct() 计算和输出标点符号
- 替换字符串C++标点符号
- 在字符串C++中查找标点符号
- 删除标点符号和空间的问题
- 如何在不推翻标点符号的情况下将琴弦倒在适当的位置?C
- 如何忽略空格和标点符号
- 如何从用户那里获得输入字符串而不是示例,然后计算空格、标点符号、数字和字母.C++
- C 删除某些符号/标点符号
- 如何在考虑标点符号的同时在文件中查找字符串
- 提升::精神::lex - 标点符号的问题
- 如何使用嵌套的getline()函数来获取字符串中的特殊字符和标点符号
- 如何删除字符串中的标点符号
- C++标点符号
- C++:字符串索引和保留标点符号时出现问题
- 在 c++ 中删除标点符号和空格的最佳方法是什么