前置增量(++x)vs后置增量(x++)
**** Hidden Message ***** 好吧,现在这是有道理的。有一次我想向增量列表添加一个数字,但不想从零开始,所以我这样做了:int i = 0;
foreach (Item item in Items) {
print string.format("Item {0}", i++);
}
我期望首先处理i ++,所以在第一个项目打印之前它应该变成1。但它从0开始。
我将用++i对其进行测试,看看这是否是诀窍 使用前判别法,你几乎不会陷入困境,要清楚的是,当处理迭代器时,问题从后判别法开始。
您选择使用从零开始计数的foreach(但您真的希望第一项是1)有点令人困惑。也许你可以从1开始计数,然后使用do while循环?
此外,如果您想对发布的代码非常挑剔,我也建议您使用无符号int (uint)。这是我个人努力做到的事情之一(在需要的地方使用合适的类型);我因为对我喜欢的类型马虎而惹了几次麻烦。-例如,我使用string创建了一个到处都是基类和派生类的程序,而我应该使用STRING和char[]的组合(这是在C++中)。我花了比写程序更多的时间来检查我的程序,修复所有的错误,重新编写正确的方法。从那时起,我让自己总是试着更加注意我*需要*的类型和我*使用*的东西。就像你举的例子一样。counter (i)永远不会是负的,所以我会使用一个无符号整数。 当我还在练习我的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;。
}输出:。
C#也是如此...但是你知道 就像我说的,post/preincriment是非常微观的东西,postincriment的使用只有在处理迭代器时才真正重要。使用preincriment你永远不会遇到麻烦,所以这就是我更喜欢使用的。...这是我为显示时间差而构建的一个快速测试,但它在这方面并没有真正做得很好,所以请从中获取您想要的东西(请随意忽略)。UNSIGNED ints
123
PostIncriment (unsigned): This operation took: 0.0005363 seconds.
123
Preincriment (unsigned): This operation took: 0.0001453 seconds.
STD::STRING
one two three four five six seven eight nine ten
Postincriment (string): This operation took: 0.0014165 seconds.
one two three four five six seven eight nine ten
Preincriment (string): This operation took: 0.0014661 seconds.
Custom STRING (MSTRING)
- A fast string implementation with a maximum of 8 characters
one two three four five six seven eight nine ten
Postincriment (mstring): This operation took: 0.0014406 seconds.
one two three four five six seven eight nine ten
Preincriment (mstring): This operation took: 0.0014739 seconds.UNSIGNED ints
123
PostIncriment (unsigned): This operation took: 0.0005591 seconds.
123
Preincriment (unsigned): This operation took: 0.0001560 seconds.
STD::STRING
one two three four five six seven eight nine ten
Postincriment (string): This operation took: 0.0014712 seconds.
one two three four five six seven eight nine ten
Preincriment (string): This operation took: 0.0015229 seconds.
Custom STRING (MSTRING)
- A fast string implementation with a maximum of 8 characters
one two three four five six seven eight nine ten
Postincriment (mstring): This operation took: 0.0015286 seconds.
one two three four five six seven eight nine ten
Preincriment (mstring): This operation took: 0.0014517 seconds.UNSIGNED ints
123
PostIncriment (unsigned): This operation took: 0.0008900 seconds.
123
Preincriment (unsigned): This operation took: 0.0002601 seconds.
STD::STRING
one two three four five six seven eight nine ten
Postincriment (string): This operation took: 0.0027534 seconds.
one two three four five six seven eight nine ten
Preincriment (string): This operation took: 0.0025759 seconds.
Custom STRING (MSTRING)
- A fast string implementation with a maximum of 8 characters
one two three four five six seven eight nine ten
Postincriment (mstring): This operation took: 0.0025534 seconds.
one two three four five six seven eight nine ten
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;。
}。
。
时差的可能解释:
http://www.embedded.com/design/programming-languages-and-tools/4410601/Pre-increment-or-post-increment-in-C-C-
我自己会使用var,让编译器自己解决。如果我偶然需要计数器超过int的最大值,那么我会使用uint甚至uint64。
如果您真的很挑剔,并且您只将它用于计数器,并且您知道项目的最大数量小于32,767,那么您应该使用无符号短整型,而不是uint。如果你真的很挑剔。
页:
[1]