C 如何打破while查找循环
C++ How to break a while find loop?
在此循环中,我想在evert dot(。)的后面添加新行( n)。
在每个点之后添加新线时如何打破循环?
while (alltxt.find(".") != string::npos)
alltxt.replace(alltxt.find("."), 1, ".n");
您可以使用不同的std :: string ::找到接受起始位置的超载。然后,您在发现的'.'
之前开始每个搜索。
类似的东西:
std::string::size_type pos = 0;
while((pos = s.find(".", pos)) != std::string::npos)
{
s.replace(pos, 1, ".n");
pos += 2; // move past the dot (and the extra 'n')
}
这取决于惯用的 sigtion&测试执行分配,然后 tests 结果:
// do the assignment and then test the results
(pos = s.find(".", pos)) != std::string::npos
还要注意,与std :: string :: for for pos
相当于(但不大于)s.size()
。
这是一个可能执行您想要的通用功能:
std::string& replace_all(std::string& str, const std::string& needle,
const std::string& replacement)
{
auto idx = str.find(needle, 0);
while (idx != std::string::npos) {
str.replace(idx, needle.size(), replacement);
idx = str.find(needle, idx + replacement.size());
}
return str;
}
使用从位置开始的发现过载。类似以下内容(未进行测试,只是说明性):
if(!str.empty())
{
size_t pos = 0;
while(true)
{
pos = str.find(pos, '.');
if(std::string::npos==pos)
break;
str.insert(++pos, 1, 'n');
}
}
在the()中使用条件将要求对其进行两次检查(您绝对需要在查找后检查),这样,只有一个测试可以出现。
相关文章:
- 在 for 循环中查找问题时遇到困难
- 在 c++ 中查找字符串中没有循环的数字总和
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- 优化在网格图中查找哈密尔循环的函数?
- 尝试创建一个程序来查找非二叉树的高度.最终得到一个很长的循环,没有答案
- c ++如何使用for循环查找重复项
- 尝试在图形中查找最短路径时的无限循环
- C++ 数据结构队列:使用 for 循环查找队列中最大的元素
- 查找涉及 while 循环的运行时函数
- std::在地图上查找无法正常工作并循环访问地图的键和值
- char数组反向查找使用无循环
- 尝试在自定义 QT 创建器文本编辑器中创建查找和替换循环.我似乎无法设置光标位置
- 查找 for 循环数组C++中存在最大值和最小值的值
- C 如何打破while查找循环
- C 查找无SQRT函数循环故障的平方根
- 在使用无序多映射实现的有向图中查找循环
- 在单向链表中查找循环
- 查找循环队列中的元素数
- 如何查找循环依赖项