- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的大学教授最近给了我们一项任务,要实现我们自己的智能指针类。在他用于复制字符串的样板代码中,我发现了这段漂亮的语法糖:
while (*sea++ = *river++);// C Sting copy
我进一步研究了这段代码,发现它与 strcpy.c 中的代码完全相同,并在以下 stackoverflow 问题中进一步解释了它的工作原理: How does “while(*s++ = *t++)” copy a string?
当我尝试在下面的代码中使用这种语法糖时,结果产生了垃圾并删除了存储在“river”中的字符串:
#include<iostream>
#include<cstring>
using namespace std;
void main()
{
const char *river = "water";// a 5 character string + NULL terminator
char *sea = new char[6];
while (*sea++ = *river++);
cout << "Sea contains: " << sea << endl;
cout << "River contains: " << river << endl;
}
结果:
我知道我可以使用以下代码简单地实现所需的结果:
int i = 0;
while (i<6)
{
sea[i] = river[i];
i++;
}
但这不是我想要的答案。我想知道我的 while 循环的实现或我的 char 指针的实例化有什么问题吗?
最佳答案
你正在显示垃圾,因为当你去显示它们时你的指针指向垃圾。您在循环时推进指针,但在显示数据时需要使用原始指针。
此外,您还存在内存泄漏,因为您没有释放 char[]
缓冲区。
试试这个:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const char *river = "water";// a 5 character string + NULL terminator
char *sea = new char[6];
const char *p_river = river;
char *p_sea = sea;
while (*p_sea++ = *p_river++);
cout << "Sea contains: " << sea << endl;
cout << "River contains: " << river << endl;
delete [] sea;
return 0;
}
关于c++ - 为什么 "while (*sea++ = *river++);"没有正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38759921/
当我尝试使用动态模板来指定映射时,将对象以外的所有内容都视为字符串,则River失败。 示例-我首先尝试为我的数据库清除所有先前的索引和河流-tempTest。 curl -XDELETE 'loca
我正在使用Elasticsearch 1.3.0版本。当我这样使用Wikipedia插件版本2.3.0创建河流时 PUT _river/my_river/_meta -d { "type" : "
我成功地为MongoDB创建了Elastic搜索河。 这是我使用/创建的索引: { "type": "mongodb", "mongodb": { "collectio
我安装了一张ouchdb(1.2.1),并且想要实现搜索引擎(elasticsearch) 但我想将搜索引擎单独安装。 所以我在不同的机器上安装了elascticsearch和插件(elasticse
我正在尝试使用elasticsearch搜寻器 https://github.com/codelibs/elasticsearch-river-web 我正确地执行了所有提到的步骤,但是我不知道如何启
我需要编写一些每天运行一次的索引作业,用于查询 Oracle 数据库表并为 ElasticSearch 建立索引。由于存在表依赖性,一些表首先索引,其他表其次索引。但围绕索引过程,我需要增强进入 ES
是否有人能够为我提供一种简单的方法,在我设计的只有一个敌人的游戏中实现第二艘敌方战舰,这是我目前的代码: import java.util.*; //Scanner that reads user i
我正在使用适用于 Elasticsearch 的 MySQL JPrante JDBC River 插件来索引来自 MySQL 的数据。 在记录集中,我有字段经度 (float)、纬度 (float)
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
在我的 MySQL 中: mysql> select * from cc; +----+------+ | id | name | +----+------+ | 1 | aa | | 2 |
我使用 mongodb 将登录数据存储为一个集合,例如: //userA 登录模块 mA: { "_id" : ObjectId("uuid 1"), "user" : "userA",
我已经成功地将其用于索引单个 使用JDBC河进行收集。但是,我不知道如何索引多个 从mysql收集。 这适用于单个集合: curl -XPUT 'http://localhost:9200/_rive
介绍 我目前正在为一家公司从事项目,到目前为止,一切都很好。我们正在生产中。我们已经看到我们的质量检查人员最近发现了ElasticSearch的一些奇怪行为。我们正在将ElasticSearch与Mo
我使用以下脚本设置了我的河流: curl -XPUT 'localhost:9200/_river/foo/_meta' -d '{ "type" : "jdbc", "jdbc" :
我正在尝试使用本地 MongoDB 实例制作“elasticsearch-river-mongodb”插件。 在 http://satishgandham.com/2012/09/a-complete
我正在尝试将 ElasticSearch 与 MySQL 结合使用。 JDBC River似乎正是我想要的,但我无法将数据插入 jdbc/jdbc 以外的任何地方,使用: curl -XPUT 'lo
我在 Wind River Linux 上工作。失败的依赖错误。谁能告诉我我必须做什么?从过去的一天开始我一直在尝试 root@AC-04:/home/mysql# rpm -ivh MySQL-se
我试图从礼貌中做这个练习: 一只小 Frog 想要到达河的对岸。 Frog 目前位于位置 0,想要到达位置 X。树叶从树上落到河面上。 给定一个非空的零索引数组 A,它由 N 个表示落叶的整数组成。
这可能是个愚蠢的问题,但我找不到任何相关信息。我找到了 github,但在我使用命令后没有任何反应。 所以,在我安装插件后,它工作正常,我使用这个命令来创建索引。 curl -XPUT localho
是否有任何官方的 mongodb river 可用于 elasticsearch ?我通过模块 mogoose 在 node.js 中使用 mongodb。我在 http://www.matt-rei
我是一名优秀的程序员,十分优秀!