zoukankan      html  css  js  c++  java
  • homework-08-作业2

    1. 了解Lambda的用法

    计算“Hello World!”中

    a.字母‘e’的个数

    b. 字母‘l’的个数

    代码:

    void calcEL()
    {
    	char s[100] = "Hello World!";
    	int l=strlen(s);
    	int sumE=0, sumI=0;
    	for_each(s,s+l,[&] (char c){
    		if (c=='e' || c=='E')
    			sumE++;
    		if (c=='l' || c=='L')
    			sumI++;
    	});
    	cout<<"E: "<<sumE<<", I: "<<sumI<<endl;
    }
    

      

    2. 练习使用智能指针

      打印“Hello World!”循环右移n位的结果

      Example:

        n = 1, output = “!Hello World”

      n = 3, output = “ld!Hello Wor”

    void ShrStr(char* s, int n)
    {
    	std::unique_ptr<string> ss(new string(""));
    	int rr=strlen(s)-n;
    	if (n>strlen(s))
    		rr=n%(strlen(s)+1);
    	//std::unique_ptr<char> tt(s+rr);
    	for (int i=0;i<strlen(s);i++)
    	{
    		int tn = rr+i;
    		if (tn>=strlen(s))
    			tn = (rr+i)%(strlen(s));
    		ss->operator+=(s[tn]);
    	}
    	cout<<ss->c_str()<<endl;
    }
    

      很挫的实现。

    后来发现可以用unique_ptr<char[]> ts(new char[len + 1]);ts[x]=y;操作数组。就先这样吧。

  • 相关阅读:
    实参和形参
    location对象
    区别 apply,call
    窗体之间的交互(window.opener)
    我的升级脚本总结
    Create elements
    history 对象
    函数参数的属性:callee
    发布app store流程
    【转】如何生成静态页面的五种方案
  • 原文地址:https://www.cnblogs.com/z-mac/p/3440813.html
Copyright © 2011-2022 走看看