homework 08_2 C++11新特性作业之二

时间:2022-11-27 04:20:18

---恢复内容开始---

1.使用Lambda表达式计算“hello world!”中字母e和i的数量

下面是代码:

 #include "stdafx.h"
#include<algorithm>
#include"iostream"
using namespace std; int main(int argc, _TCHAR* argv[])
{
char s[]="hello world!";
int E=;
int L=;
for_each(s,s+strlen(s),[&](char c)
{
if (c=='e' || c=='E')
E++;
if (c=='l' || c=='L')
L++;
});
cout<<E<<endl;
cout<<L<<endl;
system("pause");
return ;
}

下面是运行结果:
homework 08_2 C++11新特性作业之二

2.使用智能指针进行字符串“hello world”的右移位。

 #include "stdafx.h"
#include<algorithm>
#include"iostream"
#include<memory>
using namespace std; int main(int argc, _TCHAR* argv[])
{ char s[]="hello world!";
int len=strlen(s);
int i=;
int n=;
cin>>n;
unique_ptr<char[]> ts(new char[len + ]);
while(i<len)
{
ts[(i+n)%len]=s[i];
i++;
}
ts[len]='\0';
printf("%s",ts);
system("pause");
return ;
}

运行结果:

homework 08_2 C++11新特性作业之二