两个c++题目

来源:百度知道 编辑:UC知道 时间:2024/05/19 16:04:43
1,设计一个程序,输入一串只有数字和字符的字符串(元素个数不确定),将此字符串中的数字和字符分离出来,例如显示的结果如下:
数字有:12514521451
字符有:asdjahhuhgwe
2,定义指针数组int*a[5],对每一个指针元素配置5个空间,以形成5*5的二维数组,将该数组元素由1填到25,并将结果输出。提示:可用new和delete来配置空间

//第一题的C++ STL解答
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct IsNumber
{
bool operator()(char c)const
{
return c>='0' && c<='9';
}
};

struct PrintChar
{
void operator()(char c)const
{
cout<<c;
}
};

int main()
{
string str;
cin>>str;
string::iterator e = partition(str.begin(),str.end(),IsNumber());

cout<<"数字有";
for_each(str.begin(),e,PrintChar());
cout<<endl;

cout<<"非数字有";
for_each(e,str.end(),PrintChar());
cout<<endl;

return 0;
}
//第二题
#include <iostream>

using namespace std;

void Assign(int** p)
{
for