string数组输出
来源:百度知道 编辑:UC知道 时间:2024/05/26 23:35:07
程序如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
void print(string* months);
string months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
print(months);
return 0;
}
void print(string* months)
{
do{
cout<<*months<<endl;
}while(++months);
}
//question:函数运行到最后怎么会出异常了,要怎么改才符合题意呢(题意是要输出12月份名称)?
#include <iostream>
#include <string>
using namespace std;
int main()
{
void print(string* months);
string months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
print(months);
return 0;
}
void print(string* months)
{
do{
cout<<*months<<endl;
}while(++months);
}
//question:函数运行到最后怎么会出异常了,要怎么改才符合题意呢(题意是要输出12月份名称)?
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
void print(string* months,int n);
string months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
print(months,12);
return 0;
}
void print(string* months,int n)
{
if (n) {
cout<<*months<<endl;
print(months+1,n-1);
}
}
//---------------------------------------------------------------------------
void print(string* months)
{
int i;
for(i=0;i<12;i++)
cout<<*months++<<endl;
}
这个很容易想到的吗!
也不知你用的啥编译器,你的这个程序,哥用g++编译运行,一切ok,没出现异常,但最好别这样写~。