increment

Incrementing: x++ vs x += 1

孤街醉人 提交于 2019-11-27 05:34:23
I've read that many developers use x += 1 instead of x++ for clarity. I understand that x++ can be ambiguous for new developers and that x += 1 is always more clear, but is there any difference in efficiency between the two? Example using for loop: for(x = 0; x < 1000; x += 1) vs for(x = 0; x < 1000; x++) I understand that it's usually not that big of a deal, but if I'm repeatedly calling a function that does this sort of loop, it could add up in the long run. Another example: while(x < 1000) { someArray[x]; x += 1; } vs while(x < 1000) { someArray[x++]; } Can x++ be replaced with x += 1

Prefix/Postfix increment operators

人走茶凉 提交于 2019-11-27 03:51:16
I'm wanting to make sure I understand pass-by-value vs pass-by-reference properly. In particular, I'm looking at the prefix/postfix versions of the increment ++ operator for an object. Let's suppose we have the following class X : class X{ private: int i; public: X(){i=0;} X& operator ++ (){ ++i; return *this; } //prefix increment X operator ++ (int unused){ //postfix increment X ret(*this); i++; return ret; } operator int(){ return i; } //int cast }; First of all, have I implemented the prefix/postfix increment operators properly? Second, how memory-efficient is the postfix operator, compared

The difference between ++Var and Var++ [duplicate]

末鹿安然 提交于 2019-11-27 03:09:37
This question already has an answer here: How do the post increment (i++) and pre increment (++i) operators work in Java? 14 answers In programming, particularly in Java, what is the difference between: int var = 0; var++; and int var = 0; ++var; What repercussions would this have on a for loop? e.g. for (int i = 0; i < 10; i++) {} for (int i = 0; i < 10; ++i) {} Dónal tldr; Although both var++ and ++var increment the variable they are applied to, the result returned by var++ is the value of the variable before incrementing, whereas the result returned by ++var is the value of the variable

How do I increment a java.sql.Timestamp by 14 days?

社会主义新天地 提交于 2019-11-27 03:04:49
问题 I have an app that takes a Timestamp as a boundary for the start date and end date of a sql selection, I want to populate a hashmap with weeks this year since the first monday of the year as the values and the week number as the keys. I'm finding it really hard to work with timestamps and I don't feel very good about adding 86,400,000 seconds to it to increment the day, as this doesn't account for the leap days, hours, seconds. I plan on adding 13 days 23 hours, 59 minutes and 59 seconds to

Avoiding concurrency problems with MAX+1 integer in SQL Server 2008… making own IDENTITY value

大兔子大兔子 提交于 2019-11-27 02:37:10
问题 I need to increment an integer in a SQL Server 2008 column. Sounds like I should use an IDENTITY column, but I need to increment separate counters for each of my customers. Think of an e-commerce site where each customer gets their own incrementing order number, starting with 1. The values must be unique (per customer). For example, Customer1 (Order #s 1,2,3,4,5...) Customer2 (Order #s 1,2,3,4,5...) Essentially, I will need to manually do the work of SQL's identity function since the number

How to Copy (and increment) Multiple Instances of a File Using Batch File

烈酒焚心 提交于 2019-11-27 02:22:19
I need to create a batch file that copies a file and increments it upon placing it at the destination. Example. copy C:\TEMP\MyDoc.txt E:\MyData\ Essentially, I need this copy command to copy every time I start it (which it does now just fine). I would like it to increment the file name instead of overwrite it though. If I ran this three times or 100 times (never a certain number) I would like to see on the "MyData" folder: MyDoc.txt MyDoc(1).txt ... Or Copy (1) I'm not really sure what the syntax is for a duplicated file nor do I necessarily care. I just want to ensure that I'm not

JavaScript Calculate brighter colour

我与影子孤独终老i 提交于 2019-11-27 01:13:32
问题 I have a colour value in JS as a string #ff0000 How would I go about programatically calculating a brighter/lighter version of this colour, for example #ff4848 , and be able to calculate the brightness via a percentage, e.g. increase_brightness('#ff0000', 50); // would make it 50% brighter 回答1: function increase_brightness(hex, percent){ // strip the leading # if it's there hex = hex.replace(/^\s*#|\s*$/g, ''); // convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF` if(hex.length == 3){ hex =

What is a method that can be used to increment letters?

拜拜、爱过 提交于 2019-11-27 00:45:48
Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter? I would like to be able to do something like: "a"++; // would return "b" Simple, direct solution function nextChar(c) { return String.fromCharCode(c.charCodeAt(0) + 1); } nextChar('a'); As others have noted, the drawback is it may not handle cases like the letter 'z' as expected. But it depends on what you want out of it. The solution above will return '{' for the character after 'z', and this is the character after 'z' in ASCII, so it could be the result you're

R: += (plus equals) and ++ (plus plus) equivalent from c++/c#/java, etc.?

▼魔方 西西 提交于 2019-11-27 00:45:21
Does R have a concept of += (plus equals) or ++ (plus plus) as c++/c#/others do? Patrick Cuff No, it doesn't, see: R Language Definition: Operators Following @GregaKešpret you can make an infix operator: `%+=%` = function(e1,e2) eval.parent(substitute(e1 <- e1 + e2)) x = 1 x %+=% 2 ; x R doesn't have a concept of increment operator (as for example ++ in C). However, it is not difficult to implement one yourself, for example: inc <- function(x) { eval.parent(substitute(x <- x + 1)) } In that case you would call x <- 10 inc(x) However, it introduces function call overhead, so it's slower than

Ruby: How to iterate over a range, but in set increments?

不问归期 提交于 2019-11-26 23:54:06
问题 So I'm iterating over a range like so: (1..100).each do |n| # n = 1 # n = 2 # n = 3 # n = 4 # n = 5 end But what I'd like to do is iterate by 10's. So in stead of increasing n by 1, the next n would actually be 10, then 20, 30, etc etc. 回答1: See http://ruby-doc.org/core/classes/Range.html#M000695 for the full API. Basically you use the step() method. For example: (10..100).step(10) do |n| # n = 10 # n = 20 # n = 30 # ... end 回答2: You can use Numeric#step. 0.step(30,5) do |num| puts "number is