- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
"AAABB"。 void remove_dups(std::string& str) { -6ren">
如果你能为我澄清一些困惑,我会很高兴。我正在编写一个函数来删除字符串中的重复字符,例如"AB --> "AAABB"。
void remove_dups(std::string& str) {
std::string::iterator it = str.begin();
while (it != str.end()) {
if (*(it+1) == *it) {
str.erase(it+1);
} else {
++it;
}
}
}
我测试的时候好像能用。但是,我想知道,不应该有栅栏柱问题吗?当“it”是字符串的末尾时,if 语句查看不存在的下一个字符。根据 cplusplus.com,
尾后字符是一个理论字符,将跟在字符串中的最后一个字符之后。它不应被取消引用。 ( http://www.cplusplus.com/reference/string/string/end/ )
所以我想知道为什么我的函数似乎通过了测试,以及我如何以一种优雅的方式重写整个函数来解决 fencepost 问题。 (对我放轻松,伙计们。我是个 n00b。)
像这样重写
void remove_dups(std::string& str) {
std::string::iterator it = str.begin();
while (it != str.end()) {
if ((it+1) != str.end() && *(it+1) == *it) {
str.erase(it+1);
} else {
++it;
}
}
}
看起来不优雅。
最佳答案
So I'm wondering why my function seems to pass the tests
未定义的行为并不意味着它不会做你想做的事。它可能完全按照您的意愿行事,只是出于错误的原因。我猜测未定义的行为本身表现为让你得到 \0
字符,这不太可能与你的字符串中的其他字符进行比较。
and how I can rewrite the whole thing in an elegant way to get around the fencepost issue
有很多选择,但我个人的喜好是
if (it != str.end()) {
++it;
while (it != str.end()) {
/* compare *it and *(it-1) */
}
}
但请注意,erase
必须移动元素。假设你总共删除了 20 个字符。您会将字符串的其余部分向后移动一个字符,即 20 次。如果您稍微修改算法,则不需要这样做:
void remove_dups(std::string& str) {
std::string::iterator src = str.begin();
std::string::iterator dst = str.begin();
if (src != str.end()) {
++src;
++dst;
while (src != str.end()) {
if (*src != *(src-1)) {
*dst = *src;
++dst;
}
++src;
}
str.resize(dst - str.begin());
}
}
这种方法还有一个好处,即使 erase
在内存中移动字符串,它也能工作,这可能会导致迭代器中断。
关于c++ - cplusplus.com 说 std::string "shall not be dereferenced"的结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21060256/
C 中的字符串或字符数组必须以空字符结尾才能知道它们在哪里结束。为什么相同的规则不适用于其他类型的数组? 例如。计算机如何知道整数数组在哪里结束?整数/浮点数/ double 数组在内存中的表示方式有
我有一堆文件需要查看。这些文件中的所有行都以 ^M (\x0D) 结尾,但最后一行以 ^M^J (\x0D\x0A) 结尾。 显然,Vim 将文件类型确定为 DOS,效果是文件的整个内容是 显示在一行
以下是来自 Why's Poignant Guide to Ruby Chapter 6 的 Ruby 代码片段,他试图在 Ruby 中演示元编程: # Get a metaclass for thi
我有一个编程问题,希望有人能帮我解决。我正在尝试为一项工作任务学习 C 编程,我为自己设定了一个小项目,其中包括读取文件树,其中包括获取有关每个文件的信息的所有子目录。 我遇到的问题是我的程序不会忽略
其他章节请看: react 高效高质量搭建后台系统 系列 尾篇 本篇主要介绍 表单查询 、 表单验证 、 通知 (WebSocket)、 自动构建
我是 FFmepg 的新手,所以如果术语有误,请原谅我。 我想将文本和音频添加到现有的视频文件中。我链接在一起 2 drawText s,然后在不同的过滤器中,我想合并原始源视频的音轨和一些背景音乐。
使用grep我可以使用以下命令搜索单词的开头和结尾 grep -e '\' 这将找到I have a leg.,但找不到play allegro here。 Ripgrep(0.10.0)似乎不支持
$.ajax({ type:'GET', cache: 'false', url:"/foo/", data:{"id": pid}, success:func
我想在我的 iPhone 应用程序中处理旋转手势并在此期间旋转 imageView。在手势结束时,我想将 imageView 旋转到固定位置。所以,即。如果我将 imageView 从 0 弧度旋转到
我知道检测是一种将跟踪代码动态添加到方法中以启用跟踪和调试的技术。 我想知道这是否只是一个“跟踪”选项,硬编码到 CLR 中以仅添加跟踪代码,或者是否能够向方法添加任何代码? 例如,我想检查某个类中每
我正在查看一些专有源代码:使用库的示例程序。 代码是用 C 和 C++ 编写的,使用 make 构建系统。 每个文件都以注释掉的 [] 结尾:/*[]*/ 源文件和 #[]#对于生成文件。这可能是什么
是否可以循环文件中的每一行并检查它是如何结束的(LF/CRLF): using(StreamReader sr = new StreamReader("TestFile.txt")) { st
我从 JSF 网站的编程开始。目前所有文件都以 .xhtml 结尾。当我去http://localhost:8080/myProject/start.jsf一切正常。但是当我将文件从 start.xh
这是一个多站点问题。我有很多网站都带有.htaccess文件,其中包含多行,类似于: rewriterule ^(page-one|page-two|page-three)/?$ /index.php
真的没有简单的映射来跳转到段落的第一行或最后一行吗? { 和 } 是“独占”命令,因此它们会跳转到段落前后的空行如果有的话,但对于段落的实际第一行或最后一行(即,当段落位于缓冲区的顶部或底部时)。 这
我已经尝试了该站点上所有可能的解决方案。我以前使用过改造,过去已经解决了这个问题,但这次我无法解决。我正在使用 StackOverflow API 来获取问题及其详细信息。 API : https:/
我正在做模式匹配如下。但我没有得到正确的输出。请建议正确的代码以获得正确的输出。 代码 #! /usr/bin/perl -w my $subString = "1.3.6.1.2.1.26.2.1.
如何创建 if 语句以检查整数是否以 0 结尾? 例如,我想要一个这样的 if 语句: var test = 107; //107 is an example it'should some unkno
这个问题已经有答案了: javascript how to tell if one number is a multiple of another (6 个回答) 已关闭 3 年前。 每当秒 var
我正在遍历文件树以识别所有 .DYLIB 文件。 #!/bin/bash #script to recursively travel a dir of n levels function traver
我是一名优秀的程序员,十分优秀!