有哪些巧妙的方法可以输出 n 个项目的列表,其中之间有 (n-1) 个分隔符?
假设我们有一个包含 n 个元素的数组 (n > 0)。
我们希望输出这些元素的列表,它们之间有分隔符。
解决这个问题的常见方法是:
foreach item
(
output item
output separator
)
trim last separator
但必须这样做似乎有点混乱。
另一种方法是:
check that there is at least one element
loop
(
output element
next element, or break if no more elements
output separator
)
但我不确定它是否永远有效。
您是否看到其他聪明的方法来做到这一点,例如在 C、C++ 中?
Let's say that we have an array with n elements (n > 0).
We would like to output a list of those elements, with a separator between them.
A common approach to this problem is:
foreach item
(
output item
output separator
)
trim last separator
But it seems a bit messy to have to do that.
Another approach would be:
check that there is at least one element
loop
(
output element
next element, or break if no more elements
output separator
)
But I am not sure that it will always work.
Do you see other clever ways to do that, for example in C, C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
或对此的变体。我实在想不出还有什么办法可以不重复
output_item(i)
。or variations on this. I can't really think of how else not to repeat
output_item(i)
.有时:
更短。
Sometimes:
is shorter.
由于它被标记为与语言无关,我认为有必要指出,某些语言具有内置功能,可以让您甚至不用考虑这个问题。以这段 python 代码为例:
As it is tagged as language-agnostic I think it's important to point out, that some languages have built-in features to spare you from even thinking about this problem. Take this python code for example:
可能的 C++ 解决方案:
http://groups.google.com/group/comp.lang.c++ /msg/a746a588cedfa44b
总结:写一个
infix_ostream_iterator
,它与ostream_iterator
基本相同,只不过“separator”参数实际上是分隔符,而不是后缀到每一个项目。那么用法将是:A possible C++ solution:
http://groups.google.com/group/comp.lang.c++/msg/a746a588cedfa44b
Summary: write an
infix_ostream_iterator
, which is basically the same asostream_iterator
except that the "separator" parameter really is a separator, not a suffix to every item. Usage would then be:无论好坏,我使用计数循环,
我确信这是老式的投反对票诱饵,但它有效。
如果有人想告诉我这效率低下,我已经准备好火焰了吗;-)
For better or worse, I use counted loops, with
I'm sure this is downvote-bait for being old fashioned, but it works.
If anybody wants to tell me it's inefficient, boy do I have a flame ready ;-)
这个版本避免了任何额外的分支:(
对于那些害怕
goto
的人,可以使用Duff设备中的方法来编写它)This version avoids any additional branches:
(For those who are afraid of
goto
, it can be written using approach from Duff's device)Haskell 中的“第一次检查”习惯用法:
它的用法如下:
The "first check" idiom in Haskell:
It's used like so:
我总是使用第一项检查习惯用法。这是java中的代码:
I always use first item check idiom. Here is the code in java:
在 Common Lisp 中,只要您可以对分隔符进行硬编码,它就非常简单。
调用时,它返回一个由
list
中的元素组成的字符串,以“,”分隔(最后一个元素后面不跟任何内容除外)。好吧,如果输入一个输出流,它会将其打印到流中,恰好“nil”意味着“没有流,只返回一个字符串”。In Common Lisp, it's bordering on simple, as long as you can hard-code the separator.
When called, this returns a string consisting of the elements in
list
, separated by ", " (except for the last element not being followed by anything). Well, if fed an output stream, it prints it to the stream, it just so happens that 'nil' means "No stream, just return a string".