输出一个数字数组,而不必单独输出其中的每个值
Output an array of numbers without having to output each value in it separately?
我已经通过"cout"数字[1]、数字[2]等输出了数组中的每个值。我想知道是否有可能一次"计数"一个值代表数组中的所有数字。
int numbers [ ] = { 40, 20, 50, 60, 10, 15 } ;
你可以使用循环,例如
for( int const x : numbers )
{
cout << x << endl;
}
您可以使用std::copy()
与std::begin()
和std::end()
辅助函数来为您的数组创建范围。然后使用std::ostream_iterator<int>
放置输出:
#include <algorithm>
#include <iterator>
std::copy(std::begin(numbers),
std::end(numbers), std::ostream_iterator<int>(std::cout));
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 用C++在单独的头文件中完成函数体
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 处理多个子目录中的 Makefile 依赖项并输出到单独的子目录
- 如何从一个函数中获取2个单独的输出
- 如何将 "kill -3 <jvm pid>" 标准输出单独重定向到 linux 和 unix 中的文件?
- 我应该如何解析数据从输入.mov文件和输出解析数据到单独的.txt文件
- (c++)将任意文件读取为二进制文件,并将数据作为单独的文本文件输出
- 输出一个数字数组,而不必单独输出其中的每个值