Lambda表达式
// homework-09.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string> #include <memory> using namespace std; template<typename Func> int alNum(char* ins,char alp,Func func){ int count=0; int i=0; int len=strlen(ins); for(;i<len;i++) if(func(ins[i],alp)) count++; return count; } int _tmain(int argc, _TCHAR* argv[]) { unique_ptr<char[]> ins(new char[81]); gets(ins.get()); auto func=[](char ina, char coma){return ina==coma;}; char al; al='e'; cout<<"Num of:"<<al<<"\n"<<alNum(ins.get(),al,func)<<"\n"; al='l'; cout<<"Num of:"<<al<<"\n"<<alNum(ins.get(),al,func)<<"\n"; system("pause"); return 0; }
智能指针使用
#include "stdafx.h" #include <iostream> #include <string> #include <memory> using namespace std; unique_ptr<char[]> moveRight ( char* ins,int n){ int len = strlen(ins); n%=len; unique_ptr<char[]> temps(new char[len+1]); strncpy(temps.get()+len-n,ins,n); strncpy(temps.get(),ins+n,len-n); temps[len]='\0'; return temps; } int _tmain(int argc, _TCHAR* argv[]) { int n; cin>>n; unique_ptr<char[]> ins(new char[81]); cin>>ins.get(); ins = moveRight(ins.get() , n); cout<<ins.get(); system("pause"); return 0; }