- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个简单的函数,它检查给定的字符串是否符合特定条件,然后根据作为参数收到的 2 个字符串生成第三个字符串。第三个字符串很好,但是当我返回它时,它突然变成了“\n
”。
string sReturn = "";
if (sText.size() != sPassword.size()) {
//Checks to see if the texts match a condition
return sReturn;
}
for (int foo = 0; foo < sText.size(); foo++) {
sReturn = "";
sReturn += (char)sText[foo] ^ (char)sPassword[foo];
}
return sReturn;
在 for sReturn
中很好并且有正确的内容,但是一旦它存在循环,调试器突然告诉我它的内容是“\n
” .我做错了什么?
最佳答案
你不必初始化字符串使用空字符数组,如:
std::string sReturn = "";
Default constructor打算这样做对你来说效率更高。正确代码:
std::string sReturn;
将空字符串分配给 sReturn在循环中的每次迭代中不正确。更不用说清除你必须调用的字符串 std::string::clear () :
sReturn = "";
正确的代码:
sReturn.clear ();
但这应该从在你的情况下循环。
不需要明确说明转换运算符 [] 的结果(size_t) 变成一个字符,因为它是一个字符:
sReturn += (char)sText[foo] ^ (char)sPassword[foo];
正确的代码:
sReturn += sText[foo] ^ sPassword[foo];
使用 post-increment为了循环是没有必要的。它使一个每个“foo”的额外拷贝增量:
for (int foo = 0; foo < sText.size(); foo++)
这可能会被优化编译器,但你必须摆脱这个坏习惯。使用 pre-increment反而。正确的代码:
for (int foo = 0; foo < sText.size(); ++foo)
调用 std::string::size ()字符串大小时的每次迭代不改变是没有效率的:
for (size_t foo = 0; foo < sText.size(); ++foo)
更好的代码:
for (size_t foo = 0, end_foo = sText.size(); foo < end_foo; ++foo)
注意size_t类型。你不能存储32 位有符号整数的字符串大小因为它没有足够的能力存储大量。正确的类型是size_t,由返回std::string::size () 方法。
考虑到以上所有因素,正确的函数应该是这样的:
std::string
getMixedString (const std::string & text, const std::string & password)
{
std::string result;
if (text.length () != password.length ())
return result;
for (size_t pos = 0, npos = text.length (); pos < npos; ++pos)
result += text[pos] ^ password[pos];
return result;
}
但是如果你希望最终的字符串是人类可读的,那就有问题了。使用eXclusive OR两个 ASCII 上的 (XOR) 运算符字符可能会或可能不会为您提供人类可读的字符,甚至是 ASCII特点。因此,您最终可能会得到包含换行符、不可读字符、一些 garbage 的结果字符串。无论如何。
要解决这个问题,您必须想出一些更好的算法来根据其他两个字符串生成一个字符串。例如,您可以使用 MD5两个字符串的哈希值或在 base64 中对它们进行编码.
祝你好运!
关于c++ - 退出循环时字符串的内容发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3532493/
我目前正在为 Cocoa/Objective-C 项目编写一个脚本来完全自动化我的编译-运行-调试过程。 我的最后一行代码是: lldb -f Build/MyApp.app -o "run" 这实际
我有一个带有登录屏幕的脚本,如果按下取消按钮,我想完全退出该应用程序。我尝试了 3 种方法: 系统退出() QApplication.quit() QCoreApplication.instance(
我有一个 Flash 应用程序,可以重定向到另一个页面。我很乐意捕获任何其他窗口卸载事件(单击链接/提交表单)并警告用户他们将丢失 Flash 应用程序中的进度。 但是,我找不到任何方法来判断 URL
我正在尝试在 Ubuntu 上用 Python 编写一个简单的程序,它将在播放视频完成后关闭/退出/退出 VLC Player。 能否请您指导我应该在我的程序中添加什么以获得我需要的结果。 impor
我在 Lynda.com 上学习 PHP 2 视频时遇到了一个问题,因为讲师似乎忽略了告诉我们他在视频中执行的步骤之一。我在这里上传了相关视频http://www.youtube.com/watch?
某天在群内有同学问到,在python下我用input或者raw_input都得输入完后回车才能获取到输入的值,那如何实现任意键退出暂停等功能呢,我当时也没有多想,因为接触python时间也不算长,主
我按顺序调用了几个函数(我无法编辑),但有些函数会重定向用户,所以我永远不会进入下一个函数。 我正在调用一个第三方函数,它调用了我能够阻止的 wp_redirect(),但是下一行是 exit;我不知
终止/退出主函数的 D 方式是什么? import std.stdio; import core.thread; void main() { int i; while (i <= 5)
我正在申请写作。用户可以打开应用程序、写一些文本、保存他们的工作等。 我试图做到这一点,以便点击 window close按钮将提示用户 (a) 保存他们的工作(如有必要)或 (b) 退出。 我正在尝
我正在通过在 repl 中检查别人的代码来玩弄它。 它不断调用 System/exit,这导致我的 repl 崩溃。这真是令人气愤。 在我有权访问的所有代码中,我都模拟了调用。 但它也会调用一些我没有
我正在使用 subprocess执行mimic的模块程序(指定 here )。下面的代码成功地读取了一些文本并写入了一个 mp3 文件。 import subprocess proc = subpro
退出 .then 范围后数组上的值被清除 在下面的代码中tableValues1.length 给我正确的长度,直到它位于每个循环内当它退出时,作用域数组长度为零。 请谁能帮我解决这个问题 - 谢谢
我正在尝试为 s3cmd 编写一个 docker 镜像。当我通过 docker-compose 运行从 Dockerfile 构建的图像时,容器在 docker compose run 命令之前退出。
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Quitting an application - is that frowned upon? 我编写了一个 And
我遇到 Selenium WebDriver 仅执行一次后退出 for 循环的问题。据推测,这是获取内容和在加载页面之前启动循环的问题。是否有可能让 webdriver 等待页面加载? List al
#include #include #include #include "Player.h" using namespace std; void PlayerMenu(); int main()
class Test{ public static void main(String args[]) { Patron list[] = new PatronData(
我正在做一些作业,遇到了这个问题。 Write a program that reads several lines of text and prints a table indicating the
我正在用 C 创建一个简单的 Linux 命令 shell。我无法理解我的代码在哪里出现问题。 “commands”是我希望作为一个父进程的子进程同时执行的 Linux 命令字符串列表。当所有执行完成
我的控制台应用程序有点问题。应用程序应该从用户那里获取数字并将它们添加到列表中,但是如果输入是“c”,它应该关闭。我不知道如何在不使用 Scanner.nextLine() 挂起应用程序并退出循环的情
我是一名优秀的程序员,十分优秀!