increment

Thread-safe way to increment and return an integer in Delphi

China☆狼群 提交于 2020-01-03 06:53:09
问题 In a single-threaded application I use code like this: Interface function GetNextUID : integer; Implementation function GetNextUID : integer; const cUID : integer = 0; begin inc( cUID ); result := cUID; end; This could of course be implemented as a singleton object, etc. - I'm just giving the simplest possible example. Q: How can I modify this function (or design a class) to achieve the same result safely from concurrent threads? 回答1: You can use the Interlocked* functions: function

Javascript increment while assigning

别说谁变了你拦得住时间么 提交于 2020-01-02 03:55:07
问题 I was having a conversation about the prefix increment operator, and we seem to have run into a disagreement. When running this code: var x = 0; x = ++x; is the second line equivalent to: x = (x = x + 1) OR x = (x + 1) It is hard to tell the difference because the results are identical (both result in x having a value of 1) I believe that the value is not saved to the original variable when the left hand side of the assignment is the variable itself. My counterpart disagrees and thinks the

Generate vector of a repeated string with incremental suffix number

烂漫一生 提交于 2020-01-02 03:24:30
问题 I would like to generate a vector based on repeating the string "FST" but with a number at the end which increments: "Fst1" "Fst2" "Fst3" "Fst4" ... "Fst100" 回答1: An alternative to paste is sprintf , which can be a bit more convenient if, for instance, you wanted to "pad" your digits with leading zeroes. Here's an example: sprintf("Fst%d", 1:10) ## No padding # [1] "Fst1" "Fst2" "Fst3" "Fst4" "Fst5" # [6] "Fst6" "Fst7" "Fst8" "Fst9" "Fst10" sprintf("Fst%02d", 1:10) ## Pads anything less than

PHP Increment by Half

ぃ、小莉子 提交于 2020-01-01 09:02:51
问题 I have a quick question, which is probably easy to answer. I've goolged around, but not sure if I am searching correctly or what. Anyway, using PHP, how can I increment by halves? For example, I know I can use the following loop: <?php for ($i=1; $i<21; $i++) { print($i); } And it will print 1 - 20. But, how can I get it to output something like the following: 1 1.5 2 2.5 etc... Sorry for my ignorance on this, I'm just not sure how to go about it. Thanks! 回答1: Change $i++ to $i += 0.5 . Also,

PHP Increment by Half

杀马特。学长 韩版系。学妹 提交于 2020-01-01 09:02:29
问题 I have a quick question, which is probably easy to answer. I've goolged around, but not sure if I am searching correctly or what. Anyway, using PHP, how can I increment by halves? For example, I know I can use the following loop: <?php for ($i=1; $i<21; $i++) { print($i); } And it will print 1 - 20. But, how can I get it to output something like the following: 1 1.5 2 2.5 etc... Sorry for my ignorance on this, I'm just not sure how to go about it. Thanks! 回答1: Change $i++ to $i += 0.5 . Also,

C# - increment number and keep zeros in front

孤者浪人 提交于 2020-01-01 04:19:07
问题 I need to make a 40 digit counter variable. It should begin as 0000000000000000000000000000000000000001 and increment to 0000000000000000000000000000000000000002 When I use the int class, it cuts off all the zeros. Problem is I need to increment the number and then convert it to a string, with the correct number of leading zeros. The total size should be 40 digits. So if I hit 50 for example, it should look like this: 0000000000000000000000000000000000000050 How can I do that and retain the

How to increment a number by 2 in a PHP For Loop

那年仲夏 提交于 2020-01-01 03:52:31
问题 The following is a simplified version of my code: <?php for($n=1; $n<=8; $n++): ?> <p><?php echo $n; ?></p> <p><?php echo $n; ?></p> <?php endfor; ?> I want the loop to run 8 times and I want the number in the first paragraph to increment by 1 with each loop, e.g. 1, 2, 3, 4, 5, 6, 7, 8 (this is obviously simple) However, I want the number in the second paragraph to increment by 2 with each loop, e.g... 1, 3, 5, 7, 9, 11, 13, 15 I can't figure out how to make the number in the second

Behavior of increment operator at bounds for character type

耗尽温柔 提交于 2019-12-31 03:23:07
问题 I wonder how C++ behaves in this case: char variable = 127; variable++; In this case, variable now equals to -128. However did the increment operator wrapped the value to its lower bound or did an overflow occurred? 回答1: An overflow occured and results in undefined behavior . Section 5.5: Ifduring the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type , the behavior is undefined [...] The standard goes on to note that

Typescript: increment number type

柔情痞子 提交于 2019-12-31 02:55:17
问题 Is it possible from number type T to get number type Y that has value of T+1. type one = 1 type Increment<T extends number> = ??? type two = Increment<one> // 2 P.S. Currently, I have hardcoded interface of incremented values, but the problem is hardcoded and hence limited: export type IncrementMap = { 0: 1, 1: 2, 2: 3, 回答1: I would just hardcode it like this: type Increment<N extends number> = [ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35

xslt conditional increment

烈酒焚心 提交于 2019-12-31 02:36:05
问题 I'm having problems incrementing a counter under certain conditions. Input: <Users> <User> <id>1</id> <username>jack</username> </User> <User> <id>2</id> <username>bob</username> </User> <User> <id>3</id> <username>bob</username> </User> <User> <id>4</id> <username>jack</username> </User> </Users> Wanted Output: <Users> <User> <id>1</id> <username>jack01</username> </User> <User> <id>2</id> <username>bob01</username> </User> <User> <id>3</id> <username>bob02</username> </User> <User> <id>4<