乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 103|回复: 7

前置增量(++x)vs后置增量(x++)

[复制链接]

61

主题

792

帖子

35

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1015
发表于 2016-1-12 14:26:39 | 显示全部楼层 |阅读模式

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

14

主题

275

帖子

6

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
331
发表于 2016-1-13 07:09:01 | 显示全部楼层
好吧,现在这是有道理的。有一次我想向增量列表添加一个数字,但不想从零开始,所以我这样做了:
  1. int i = 0;
  2. foreach (Item item in Items) {
  3. print string.format("Item {0}", i++);
  4. }

我期望首先处理i ++,所以在第一个项目打印之前它应该变成1。但它从0开始。
我将用++i对其进行测试,看看这是否是诀窍
回复

使用道具 举报

61

主题

792

帖子

35

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1015
发表于 2016-1-13 08:57:27 | 显示全部楼层
使用前判别法,你几乎不会陷入困境,要清楚的是,当处理迭代器时,问题从后判别法开始。
您选择使用从零开始计数的foreach(但您真的希望第一项是1)有点令人困惑。也许你可以从1开始计数,然后使用do while循环?
此外,如果您想对发布的代码非常挑剔,我也建议您使用无符号int (uint)。这是我个人努力做到的事情之一(在需要的地方使用合适的类型);我因为对我喜欢的类型马虎而惹了几次麻烦。-例如,我使用string创建了一个到处都是基类和派生类的程序,而我应该使用STRING和char[]的组合(这是在C++中)。我花了比写程序更多的时间来检查我的程序,修复所有的错误,重新编写正确的方法。从那时起,我让自己总是试着更加注意我*需要*的类型和我*使用*的东西。就像你举的例子一样。counter (i)永远不会是负的,所以我会使用一个无符号整数。
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2016-2-1 04:11:20 | 显示全部楼层
当我还在练习我的C印章时#include 。

int main(int argc, char *argv[])。
{。
整型 x = 3;。
int y;。
int z;。

y = ++x;。
printf(“ y(++x) = %d\n x = %d\n”, y, x);。

x = 3;// 复位 x。
z = x++;。
printf(“ z(x++) = %d\n x = %d\n”, z, x);。

返回 0;。
}输出:。
回复

使用道具 举报

57

主题

559

帖子

13

银币

中流砥柱

Rank: 25

铜币
786
发表于 2016-2-1 06:27:12 | 显示全部楼层
C#也是如此...但是你知道
回复

使用道具 举报

61

主题

792

帖子

35

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1015
发表于 2016-2-1 15:56:51 | 显示全部楼层
就像我说的,post/preincriment是非常微观的东西,postincriment的使用只有在处理迭代器时才真正重要。使用preincriment你永远不会遇到麻烦,所以这就是我更喜欢使用的。...这是我为显示时间差而构建的一个快速测试,但它在这方面并没有真正做得很好,所以请从中获取您想要的东西(请随意忽略)。
  1. UNSIGNED ints
  2. 123
  3. PostIncriment (unsigned): This operation took: 0.0005363 seconds.
  4. 123
  5. Preincriment (unsigned): This operation took: 0.0001453 seconds.
  6. STD::STRING
  7. one two three four five six seven eight nine ten
  8. Postincriment (string): This operation took: 0.0014165 seconds.
  9. one two three four five six seven eight nine ten
  10. Preincriment (string): This operation took: 0.0014661 seconds.
  11. Custom STRING (MSTRING)
  12.          - A fast string implementation with a maximum of 8 characters
  13. one two three four five six seven eight nine ten
  14. Postincriment (mstring): This operation took: 0.0014406 seconds.
  15. one two three four five six seven eight nine ten
  16. Preincriment (mstring): This operation took: 0.0014739 seconds.
  1. UNSIGNED ints
  2. 123
  3. PostIncriment (unsigned): This operation took: 0.0005591 seconds.
  4. 123
  5. Preincriment (unsigned): This operation took: 0.0001560 seconds.
  6. STD::STRING
  7. one two three four five six seven eight nine ten
  8. Postincriment (string): This operation took: 0.0014712 seconds.
  9. one two three four five six seven eight nine ten
  10. Preincriment (string): This operation took: 0.0015229 seconds.
  11. Custom STRING (MSTRING)
  12.          - A fast string implementation with a maximum of 8 characters
  13. one two three four five six seven eight nine ten
  14. Postincriment (mstring): This operation took: 0.0015286 seconds.
  15. one two three four five six seven eight nine ten
  16. Preincriment (mstring): This operation took: 0.0014517 seconds.
  1. UNSIGNED ints
  2. 123
  3. PostIncriment (unsigned): This operation took: 0.0008900 seconds.
  4. 123
  5. Preincriment (unsigned): This operation took: 0.0002601 seconds.
  6. STD::STRING
  7. one two three four five six seven eight nine ten
  8. Postincriment (string): This operation took: 0.0027534 seconds.
  9. one two three four five six seven eight nine ten
  10. Preincriment (string): This operation took: 0.0025759 seconds.
  11. Custom STRING (MSTRING)
  12.          - A fast string implementation with a maximum of 8 characters
  13. one two three four five six seven eight nine ten
  14. Postincriment (mstring): This operation took: 0.0025534 seconds.
  15. one two three four five six seven eight nine ten
  16. Preincriment (mstring): This operation took: 0.0028705 seconds.
源代码(减去自定义类和东西),#包含"D:\编程\C++\项目\包含\pctimer. h"。
#包含"D:\编程\C++\项目\include\mstring. h"。

int主 () {。
std::cout.setf(std::ios_base::f混合,std::ios_base::floatfield);。
pctimer_tt1, t2;。
t1=pctimer();。

//未签名。
//。
std::c。

std::向量项;。

items.push_back(1)。
items.push_back(2)。
items.push_back(3)。

for(std::向量::迭代器i=items.begin(); i!=items.end(); i++) {。
std::c。
}。

std::cout.precision(7);。
t2=pctimer();。
std::c。

//重置计时器,。
t1=pctimer();。

for(std::向量::迭代器i=items.begin(); i!=items.end (); ++i){。
std::c。
}。

std::cout.precision(7);。
t2=pctimer();。
std::c。

STD::字符串。
//。
std::c。

std::向量string_items;。

string_items.push_back("一");。
string_items.push_back("两个");。
string_items.push_back("三");。
string_items.push_back(“四”);。
string_items.push_back(“五”);。
string_items.push_back(“六”);。
string_items.push_back(“七”);。
string_items.push_back(“八”);。
string_items.push_back(“九”);。
string_items.push_back("十");。

//重置计时器,。
t1=pctimer();。

for(std::向量::迭代器i=string_items.begin(); i!=string_items.end(); i++) {。
std::c。
}。

std::cout.precision(7);。
t2=pctimer();。
std::c。

//重置计时器,。
t1=pctimer();。

for(std::向量::迭代器i=string_items.begin(); i!=string_items.end (); ++i){。
std::c。
}。

std::cout.precision(7);。
t2=pctimer();。
std::c。


//自定义字符串(MSTRING)。
//。
std::c。
std::向量mstring_items;。

mstring_items.push_back("一");。
mstring_items.push_back("两个");。
mstring_items.push_back("三");。
mstring_items.push_back(“四”);。
mstring_items.push_back(“五”);。
mstring_items.push_back(“六”);。
mstring_items.push_back(“七”);。
mstring_items.push_back(“八”);。
mstring_items.push_back(“九”);。
mstring_items.push_back("十");。

//重置计时器,。
t1=pctimer();。

for(std::向量::迭代器i=string_items.begin(); i!=string_items.end(); i++) {。
std::c。
}。

std::cout.precision(7);。
t2=pctimer();。
std::c。

//重置计时器,。
t1=pctimer();。

for(std::向量::迭代器i=string_items.begin(); i!=string_items.end (); ++i){。
std::c。
}。

std::cout.precision(7);。
t2=pctimer();。
std::c。

返回0;。
}。

回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2016-2-1 16:02:04 | 显示全部楼层
时差的可能解释:
http://www.embedded.com/design/programming-languages-and-tools/4410601/Pre-increment-or-post-increment-in-C-C-
回复

使用道具 举报

61

主题

792

帖子

35

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1015
发表于 2016-2-1 16:06:11 | 显示全部楼层

我自己会使用var,让编译器自己解决。如果我偶然需要计数器超过int的最大值,那么我会使用uint甚至uint64。
如果您真的很挑剔,并且您只将它用于计数器,并且您知道项目的最大数量小于32,767,那么您应该使用无符号短整型,而不是uint。如果你真的很挑剔。
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-4 21:51 , Processed in 0.205660 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表