如何更改分隔符的位置?
此示例:
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int v[] = { 1, 2, 3 };
std::copy( &v[0], &v[3], std::ostream_iterator< int >( std::cout, "\n " ) );
}
产生下一个输出:
1
2
3
有没有办法更改示例以使其产生下一个输出?
1
2
3
PS 我知道我可以使用 for 循环,但我对使用算法和迭代器的解决方案感兴趣。
This example :
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int v[] = { 1, 2, 3 };
std::copy( &v[0], &v[3], std::ostream_iterator< int >( std::cout, "\n " ) );
}
produces next output :
1
2
3
Is there a way to change the example to make it produce next output?
1
2
3
PS I know I could use the for loop, but I am interested in a solution that uses algorithms and iterators.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
std::ostream_iterator
无法做到这一点。 (恕我直言,应该有是,但它不在那里。)如果你不介意写一个额外的小
函数或类,
您可以使用
std::transform
,例如:如果您使用 C++11,则可以对
FormatString
使用 lambda。我发现这种需要经常发生,所以我写了一个
PatsubstTransformer
——一个功能对象,基本上实现 GNU make 的
$(patsubst...)
函数。所以我就必须写:
我发现我经常使用这个。 (我还发现使用
std::transform
更多比
std::copy
合适,因为我输出的是转变。)
There is no way to do this with
std::ostream_iterator
. (IMHO, there shouldbe, but it's not there.) If you don't mind writing an extra small
function or class,
you can use
std::transform
, e.g.:If you have C++11, you can use a lambda for the
FormatString
.I find the need for this occurs often enough that I've written a
PatsubstTransformer
—a functional object which basicallyimplements the
$(patsubst...)
function of GNU make. So I would justhave to write:
I find I use this a lot. (I also find using
std::transform
moreappropriate than
std::copy
, since what I'm outputting is atransformation.)
如果你想使用 C++11,你可以使用 lambda。
例如这样:
If you want to use C++11 you can use a lambda.
e.g like this:
使用 std::cout << " ",而不是
std::cout
为:这里的表达式
std::cout << " "
首先评估将一个单个空格打印到输出,并将评估值std::ostream&
传递给std: :ostream_iterator
现在输出将正确对齐:
工作代码:http://www.ideone.com/kSdpk
顺便说一下,不要写
&v[3]
。这会调用未定义行为。写入v+3
。Use
std::cout << " "
, instead ofstd::cout
as:Here the expression
std::cout << " "
first evaluates which prints a single space to the output, and the evaluated value which isstd::ostream&
gets passed tostd::ostream_iterator
Now the output will be aligned correctly:
Working code : http://www.ideone.com/kSdpk
By the way, don't write
&v[3]
. That invokes Undefined bevahior. Writev+3
.在
std::copy
之前输出一个空格。Output a single space before the
std::copy
.不,不是真的。
ostream_iterator
不可这样配置。因此,您必须使用其他答案中找到的前置空格“解决方法”,并手动截断最后一行。
顺便说一句 已经注意到,严格来说,
&v[3]
会由于子表达式v[3]
中的隐式取消引用而调用未定义的行为。更喜欢&v[0]+3
(或只是v+3
)——“拥有”一个指向数组末尾一位的指针是可以的,只要它没有取消引用。您可以创建自己类型的
ostream_iterator
来执行此操作,如以下示例所示。是的,它很冗长;但是,您也可以根据自己不断变化的需求进行更改:
(我使用
[n3290: 24.6/2]
来确定此功能所需的成员和基本规范并符合标准。)现场演示。
No, not really.
ostream_iterator
isn't configurable like that.So you'll have to use the pre-space "workaround" as found in other answers, and manually chop off that final line.
BTW It's been noted that
&v[3]
, strictly speaking, invokes undefined behaviour due to the implicit dereference in the sub-expressionv[3]
. Prefer&v[0]+3
(or justv+3
) — "having" a pointer to one-past-the-end of an array is okay, as long as it's not dereferenced.You could make your own kind of
ostream_iterator
that does this, as the following example demonstrates.Yes, it's verbose; however, you can also change it around however you like to suit your changing needs:
(I used
[n3290: 24.6/2]
to determine the members and base-specification required for this to work and to be standard-compliant.)Live demo.