使用裸指针:

#include <iostream>
#include <memory>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std;
int test(int n)
{
char * p(new char[13]);
n = 12-n;
strcpy(p,"hello world!");
string s(p+n);
*(p+n) = '\0';
string t(p);
cout<<s<<t<<endl;
return 0;
}//没有delete操作,观察执行100000次的结果
int main()
{
for (int i=0;i<100000;i++)
{
test(1);
}
return 0;
}

使用智能指针:

#include <iostream>
#include <memory>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std;
int test(int n)
{
shared_ptr<char> p(new char[13]);
n = 12-n;
strcpy(p.get(),"hello world!");
string s(p.get()+n);
*(p.get()+n) = '\0';
string t(p.get());
cout<<s<<t<<endl;
return 0;
}//没有delete操作,观察执行100000次的结果
int main()
{
for (int i=0;i<100000;i++)
{
test(1);
}
return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s("hello world!");
string::iterator idx;
int suml=0,sume=0;
for_each (s.begin(),s.end(),[&suml,&sume](char x)
{
suml+=x=='l'?1:0;
sume+=x=='e'?1:0;
});
cout<<suml<<endl;
cout<<sume<<endl;
return 0;
}

work8的更多相关文章

  1. 3、RabbitMQ-work queues 工作队列

    work queues 工作队列 1.模型图: 为什么会出现 work queues? 前提:使用 simple 队列的时候 我们应用程序在是使用消息系统的时候,一般生产者 P 生产消息是毫不费力的( ...

  2. uoj #190. 【集训队互测2016】消失的源代码 提交答案题

    Test 1: 发现是一个字母表的映射 把 \('a' \to 'z'\) 打进去找出映射就好了QAQ . Test 2: 求助 \(dalao\) 得知的点.. 答案是 : \(2016x^2 + ...

  3. Day 14 python 之 字符串练习

    一.字符串总结与练习 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "DaChao" # Date: ...

  4. 线程池&进程池

    线程池&进程池 池子解决什么问题? 1.创建/销毁线程伴随着系统开销,如果过于频繁会影响系统运行效率 2.线程并发数量过多,抢占系统资源,从而导致系统阻塞甚至死机 3.能够刚好的控制和管理池子 ...

  5. HZOI20190821模拟28题解

    题面:https://www.cnblogs.com/Juve/articles/11390839.html 所有官方正解在我的文件里 A. 虎 算法1:我们发现非关键边与黑色边去掉以后,答案就是将所 ...

  6. 【python基础语法】国庆扩展练习题

    ''' 一.国庆知识小拓展 1. 用户登陆程序需求: 1. 输入用户名和密码; 2. 判断用户名和密码是否正确? (name='root', password='123') 3. 为了防止暴力破解, ...

  7. RabbitMQ传输原理、五种模式

    本文代码基于SpringBoot,文末有代码连接 .首先是一些在Spring Boot的一些配置和概念,然后跟随代码看下五种模式 MQ两种消息传输方式,点对点(代码中的简单传递模式),发布/订阅(代码 ...

随机推荐

  1. 你听说过PHP 的面向方面编程吗?

    面向方面编程(AOP)对于PHP来说是一个新的概念.现在PHP对于 AOP 并没有官方支持,但有很多扩展和库实现了这个特性.本课中,我们将使用 Go! PHP library 来学习 PHP 如何进行 ...

  2. (1)WinForm和WebForm

    如上所述,WinForm程序通常分为四层,而WebForm程序通常分为三层.   举例说明,winForm程序: IEnumerable<BatchUpdateResult<string, ...

  3. Android权限安全(7)binder,service,zygote安全相关简介

    binder 提供服务的service中的binder thread 检查调用者的uid 不是root,system就异常. service 也检查调用者的uid 不是root,system,只能注册 ...

  4. BZOJ 2004 Bus 公交线路(矩阵)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2004 题意:小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依 ...

  5. Python之格式化输出讲解

    1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...

  6. ruby安装插件报错

    有时候我们需要安装ruby插件的时候,会报如下错误:  ERROR: Could not find a valid gem 'rdiscount' (>= 0), here is why: Un ...

  7. Android自动化测试之Monkeyrunner从零开始(三)

    转自http://www.51testing.com/html/81/22381-854342.html 时光过得太快了,一晃离上一篇monkeyrunner系列的博客已经一年多了.这一年多时间经历了 ...

  8. sscanf() 和 sprintf()的用法。

    因为感觉比较有用. 这几次比赛,用过几次,所以写个程序,总结一下. 如果用sscanf(s, "%d.%d", &a, &b); 的时候,一定要注意是否s里一定有小 ...

  9. UVa 10341 (二分求根) Solve It

    很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可. #include <cstdio> #include <cmath> //using namespa ...

  10. WebView点击加载的页面中的按钮时不弹出新窗口以及在加载后执行javascript

    mWebView.setWebViewClient(new WebViewClient() { //点击网页中按钮时,在原页面打开 public boolean shouldOverrideUrlLo ...