Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何输出具有[0]-a[0]a[1]-a[0]a[1]a[2]的阵列_C++_Arrays - Fatal编程技术网

C++ 如何输出具有[0]-a[0]a[1]-a[0]a[1]a[2]的阵列

C++ 如何输出具有[0]-a[0]a[1]-a[0]a[1]a[2]的阵列,c++,arrays,C++,Arrays,如何打印带有[0]然后是[0]、[1]然后是[0]、[1]、[2]的数组。。。。 例如 我有一个数组是[]={1,2,3,4} 我的输出应该是: 1. 12 123 1234 这是我的代码,但输出错误: #include <iostream> using namespace std; int main() { int a[] = { 1,2,3,4 }; for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {

如何打印带有[0]然后是[0]、[1]然后是[0]、[1]、[2]的数组。。。。 例如 我有一个数组是[]={1,2,3,4} 我的输出应该是: 1. 12 123 1234 这是我的代码,但输出错误:

#include <iostream>
using namespace std;
int main()
{
    int a[] = { 1,2,3,4 };
    for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
    {
        for (int j = 0; j < sizeof(a) / sizeof(a[0]); j++)
        {
            cout << a[i] << endl;
        }
    }     
    system("pause");
}
#包括
使用名称空间std;
int main()
{
int a[]={1,2,3,4};
对于(int i=0;icout您可能应该做的是迭代i,因为它包含您在数组中的当前位置。然后您可以打印数组直到第i个位置。如下所示:

int main()
{
    int a[] = {1,2,3,4};
    for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
    {
        for (int j = 0; j <= i ; j++)
        {
            cout << a[j];
        }
        cout << endl;       
    }
}
intmain()
{
int a[]={1,2,3,4};
对于(int i=0;i
intmain()
{
int a[]={1,2,3,4};
对于(int i=0;i使用
std::copy

for ( int i = 1; i <= 4; i++ )
{
   std::copy( arr, arr + i, std::ostream_iterator<int>( std::cout, "") );
   std::cout << std::endl;
}
for(int i=1;i
for ( int i = 1; i <= 4; i++ )
{
   std::copy( arr, arr + i, std::ostream_iterator<int>( std::cout, "") );
   std::cout << std::endl;
}