使用裸指针:

#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. LA 6047 Perfect Matching 字符串哈希

    一开始我用的Trie+计数,但是不是计多了就是计少了,后来暴力暴过去的…… 看了别人的代码知道是字符串哈希,但是仍有几个地方不理解: 1.26^500溢出问题 2.没考虑哈希碰撞? 跪求指点! #in ...

  2. Fucking "pkg-config not found"

    If you got pkg-config not found error in gnu auto tools then, you must install the related librarys ...

  3. 将SQLServer2005中的数据同步到Oracle中

    有时由于项目开发的需要,必须将SQLServer2005中的某些表同步到Oracle数据库中,由其他其他系统来读取这些数据.不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. ...

  4. MyBatis学习总结4--解决字段名与实体类属性名不相同的冲突

    在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定是完全相同的,如果直接在xml映射文件中使用sql进行映射,会造成返回值为空的情况,下面阐述解决方案: 测试所用表和数据 create t ...

  5. 栈中的push实现

  6. bzoj1594

    首先想到二分答案 然后我们从大往小加区间,如果之前出现了一个区间包含当前区间 那显然不合法,我们可以用并查集了维护 type node=record x,y,mi,id:longint; end; . ...

  7. LT1619EMS8 锂电池 升压电路分析

    LT1619EMS8 锂电池 升压电路分析 本文主要是分析LT1619EMSB锂电池升压芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. 2016-1-23 深圳 南山平山村 曾剑锋 一. ...

  8. Make AngularJS $http service behave like jQuery.ajax()(转)

    There is much confusion among newcomers to AngularJS as to why the $http service shorthand functions ...

  9. 《C++ Primer 4th》读书笔记 第4章-数组和指针

    原创文章,转载请注明出处: http://www.cnblogs.com/DayByDay/p/3911573.html

  10. Delphi 2010 安装及调试

    呵呵,毫不客气地说,Delphi 2010 这个版本可以算是 Delphi 的一个“里程碑”,为什么这么说?因为这个版本实现了几个 Delphi 应该有却一直没有的功能 Delphi 2010 的新功 ...