一个工作的std ::复制的例子 - 打印数组 [英] A working std::copy example - printing an array

查看:125
本文介绍了一个工作的std ::复制的例子 - 打印数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是行不通的。我想学习如何使用std ::副本,但是我无法找到任何工作code。
我在GCC 4.6.1运行此。它不会做任何事情时,我打控制D.如果我打对照C ......它打印出的新线而已。

找到code位置:

打印一个数组,C ++?

 的#include<&iostream的GT;
#包括LT&;矢量>
#包括LT&;&算法GT;
#包括LT&;&迭代器GT;诠释的main()
{
    的std ::矢量<&INT GT; userInput;    //读取直到输入结束。
    //打控制用DA
    性病::复制(的std :: istream_iterator< INT>(的std :: CIN)
              的std :: istream_iterator< INT>()
              的std :: back_inserter(userInput)
             );    //正常的顺序打印
    性病::复制(userInput.begin()
              userInput.end(),
              的std :: ostream_iterator< INT>(STD ::法院)
             );
    性病::法院LT&;< \\ n;    //以相反的顺序打印:
    性病::复制(userInput.rbegin()
              userInput.rend(),
              的std :: ostream_iterator< INT>(STD ::法院)
             );
    性病::法院LT&;< \\ n;
}


解决方案

不知道你是如何运行它,或者你输入的内容,但似乎对我来说运行良好:

  PAX $ G ++ -o QQ qq.cpp; ./qq
1
2
3
4

0
9
8
7
6
< CTRL-D>
1,2,3,4,5,0,9,8,7,6,
6,7,8,9,0,5,4,3,2,1,

这是在Cygwin下的GCC 4.3.4。有一点要注意的是,(在我的环境至少), CTRL-D ,必须在新的一行输入。输入:

  1 2 3 4 5℃CTRL-D>

我(在 CTRL-D 被忽视),但没有工作:

  1 2 3 4 5
< CTRL-D>

做到了。

您可以通过执行类似绕过与 CTRL-D 这些问题:

 回声1 2 3 4 5 6 7 8 9 0 | ./qq

这样的文件为此不依赖于你的终端特性。因为它的可能的是MinGW的(作为一个Windows应用程序,而不是在Cygwin的仿真层运行)要求结束文件的Windows字符,这是特别重要的, CTRL-Z

运行此命令的行为预期:

  PAX $回声1 2 3 4 5 6 7 8 9 0 | ./qq
1,2,3,4,5,6,7,8,9,0,
0,9,8,7,6,5,4,3,2,1,

This does not work. I'm trying to learn how to use std::copy but I can't find any working code. I ran this in gcc 4.6.1. And it does not do anything when I hit control D. If I hit Control C...it prints out the new lines only.

Found code here:

Printing an array in C++?

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 

int main() 
{ 
    std::vector<int>    userInput; 

    // Read until end of input. 
    // Hit control D   
    std::copy(std::istream_iterator<int>(std::cin), 
              std::istream_iterator<int>(), 
              std::back_inserter(userInput) 
             ); 

    // Print in Normal order 
    std::copy(userInput.begin(), 
              userInput.end(), 
              std::ostream_iterator<int>(std::cout,",") 
             ); 
    std::cout << "\n"; 

    // Print in reverse order: 
    std::copy(userInput.rbegin(), 
              userInput.rend(), 
              std::ostream_iterator<int>(std::cout,",") 
             ); 
    std::cout << "\n"; 
} 

解决方案

Not sure how you're running it or what you're entering but that seems to run fine for me:

pax$ g++ -o qq qq.cpp ; ./qq
1
2
3
4
5
0
9
8
7
6
<CTRL-D>
1,2,3,4,5,0,9,8,7,6,
6,7,8,9,0,5,4,3,2,1,

This is with gcc 4.3.4 under Cygwin. One thing to watch out for is that (in my environment at least), CTRL-D has to be entered on a new line. Entering:

1 2 3 4 5<CTRL-D>

didn't work for me (the CTRL-D was ignored) but:

1 2 3 4 5
<CTRL-D>

did.

You can bypass those issues with CTRL-D by doing something like:

echo 1 2 3 4 5 6 7 8 9 0 | ./qq

so that end of file does not depend on your terminal characteristics. This is especially important since it may be that MinGW (being a Windows application rather than running under CygWin's emulation layer) requires the Windows end-of-file character, CTRL-Z.

Running this command acts as expected:

pax$ echo 1 2 3 4 5 6 7 8 9 0 | ./qq
1,2,3,4,5,6,7,8,9,0,
0,9,8,7,6,5,4,3,2,1,

这篇关于一个工作的std ::复制的例子 - 打印数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆