C++没有初始化变量

use*_*007 5 c++ logic

我的朋友们有问题.问题是没有初始化变量如何打印0到10?

我知道使用初始化使用for循环打印的方法

 for(int i=0;i<=10;i++) {
     cout<<i;
 }
Run Code Online (Sandbox Code Playgroud)

int i = 0是初始化的.那么如何在不初始化相关变量的情况下打印0到10个值?有可能吗?

Luc*_*ore 11

把事情简单化:

cout << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10";
cout << "012345678910";
Run Code Online (Sandbox Code Playgroud)

运行时递归:

void myPrint(int x)
{ 
    cout << x;
    if ( x < 10 )
       myPrint( x+1 );
}

myPrint(0);
Run Code Online (Sandbox Code Playgroud)

编译时递归:

template<int x> void foo()
{ 
    foo<x-1>(); 
    cout << x << '\n'; 
}

template<> void foo<0>() 
{ 
    cout << 0 << '\n'; 
}

foo<10>();
Run Code Online (Sandbox Code Playgroud)


Rob*_*obᵩ 11

你的朋友应该学会更准确地指出他们的问题.

int i; // i is not initialized
i = 0; // i is assigned
for( ;i<=10;i++)
{
  cout<<i;
}
Run Code Online (Sandbox Code Playgroud)