zoukankan      html  css  js  c++  java
  • homework-09

    1. 用Lambda计算"Hello World!"中字母e和l的个数:

     1 #include <iostream>
     2 using namespace std;
     3 char str[110];
     4 auto func = [] (char ch)
     5 {
     6     int num = 0;
     7     int len = strlen(str);
     8     for (int i = 0;i < len;i++) if (str[i] == ch) num++;
     9     cout<<num<<endl;
    10 };
    11 int main(int argc, const char * argv[])
    12 {
    13     strcpy(str,"Hello World!");
    14     func('e');
    15     func('l');
    16     return 0;
    17 }
    View Code

    无论如何还是成功了...

    也不知道Lambda是不是这么用...感觉参数部分应该加点东西吧...

    不过还是编译成功了...也试用了一下新的auto特性...

    总的来说和最初用的函数基本上没神马区别...

    2. 练习使用智能指针打印"Hello World!"循环右移n位的结果:

     1 #include <iostream>
     2 #include <memory>
     3 using namespace std;
     4 char str[20];
     5 void deviate(int k)
     6 {
     7     shared_ptr<char>p(new char[20]);
     8     strcpy(p.get(),"Hello World!");
     9     string str1 = p.get() + strlen("Hello World!") - k;
    10     cout<<str1;
    11     *(p.get() + strlen("Hello World!") - k) = '';
    12     str1 = p.get();
    13     cout<<str1<<endl;
    14 }
    15 int main(int argc, const char * argv[])
    16 {
    17     deviate(1);
    18     deviate(3);
    19     return 0;
    20 }
    View Code

    指针依旧是我的弱项...

    不过随着本学期高级程序设计语言2的学习...指针有望能够真正理解...

    同样...智能指针感觉还是不太会用...用的和一般指针也差不了多少...

    这次就这样吧...还得写围棋呢...

  • 相关阅读:
    冲刺(9)
    冲刺(8)
    冲刺(7)
    C#的post请求 捕获错误代码的内容
    [转载]Memory Limits for 32-bit and 64-bit processes
    IIS 503
    识别chrome浏览器
    时间同步
    修改Visual Studio启动画面授权信息
    Temporary ASP.NET Files权限问题
  • 原文地址:https://www.cnblogs.com/yimingzou/p/3440750.html
Copyright © 2011-2022 走看看