基础知识通道:http://blog.csdn.net/Xiejingfa/article/details/50955295

C/C++:

 #include <iostream>
#include <vector>
#include <string> #define allocate_length 100000 int main()
{ //allocator比new快的原因:分离分配和初始化这两个操作allocator少执行一步,new则一般执行两次(初始化和赋值); std::clock_t start = , end = ; start = clock();
std::string *str1 = new std::string[allocate_length];
auto str6=str1;
for (int i = ; i < allocate_length; i++)
{
*str1++ = "Hello World";
} delete []str6;
end = clock();
std::cout << (double(end - start) / CLOCKS_PER_SEC) << std::endl; start = clock();
std::allocator<std::string> str_allocate;
std::string *str3 = str_allocate.allocate(allocate_length); //分配20个string原始内存
auto str4=str3; //方法一:使用默认构造
for (int i = ; i < allocate_length; i++)
{
str_allocate.construct(str3++,"Hello World");
} //方法二:使用allocator的伴随算法(分别是带n与不带)
//其他算法:std::uninitialized_copy(iterator begin,iterator end,T value); //std::uninitialized_fill_n(str3,allocate_length,"Hello World"); //str_allocate.destroy()调用对象的析构,但是内存还是由allocator控制,需要自己释放
str_allocate.deallocate(str4,allocate_length);
end = clock(); std::cout << (double(end - start) / CLOCKS_PER_SEC) << std::endl; return ;
}

C++ std::allocator<T> 与new对比效率使用的更多相关文章

  1. no suitable ctr exists to convert from 'int' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >

    int xfun(int *a,int n) { int x = *a;//a的类型是int *,a+1跳动一个int的长度 ; pa < a + n; pa++)//指向同一个类型的指针比较大 ...

  2. 浅谈C++ allocator内存管理(对比new的局限性)(转)

    STL中,对内存管理的alloc的设计,迫使我去学习了allocator类.这里对allocator内存管理做了点笔记留给自己后续查阅.allocator类声明.定义于头文件<memory> ...

  3. gcc5.4报错对‘std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()’未定义的引用

    我在编译ligra是遇到了这个问题,网上搜了一遍,发现是了原因https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.ht ...

  4. 【CString与string转换】不存在从 "LPWSTR" 转换到 "std::basic_string<char, std::char_traits<char>, std::allocator(转)

    原文转自 http://blog.csdn.net/qq_23536063/article/details/52291332 [问题描述] CString cstr: sring str(cstr.G ...

  5. libraries\include\boost-1_61\boost/regex/v4/perl_matcher.hpp(362): error C2292: 'boost::re_detail_106100::perl_matcher<const char *,std::allocator<boost::sub_match<const char *>>,boost::regex_traits<c

    这个问题在Windows上基于CMake编译Caffe-SSD的GPU版时出现. 网上找到的博客贴出的解决办法是删掉regex和rv相关代码,甚至不编译detection_output_layer.c ...

  6. hive 连接查询sql对比效率

    准备4个表 从mysql 导出excel 转换为txt 创建hive 表的导入文件 create table bdqn_student( sno int, sname string, sbirthda ...

  7. error C3867: “std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str”: 函数调用缺少参数列表;请使用“&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str”创建指向成员的指针

    这个问题找了很多没有找到满意的答案.仔细看了一下,是使用了c_str的问题. 我直接把使用string.c_str的地方使用char*代替即解决问题.

  8. 实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  9. [转载] 彻底学习STL中的Allocator

    原文: http://cissco.iteye.com/blog/379093 帮助我们理解allocator的原理 Allocator是C++语言标准库中最神秘的部分之一.它们很少被显式使用,标准也 ...

随机推荐

  1. Go单元测试注意事项及测试单个方法和整个文件的命令

    Go程序开发过程中免不了要对所写的单个业务方法进行单元测试,Go提供了 "testing" 包可以实现单元测试用例的编写,不过想要正确编写单元测试需要注意以下三点: Go文件名必须 ...

  2. Django_rest_framework_基础

    简介 为什么要使用REST framework? Django REST framework 是一个强大且灵活的工具包,用以构建Web APIs. - 在线可视的API,对于赢得你的开发者们十分有用  ...

  3. Linux 下软件安装

    Linux 下软件安装 一.Linux 上的软件安装 通常 Linux 上的软件安装主要有三种方式: 在线安装 从磁盘安装deb软件包 从二进制软件包安装 从源代码编译安装 这几种安装方式各有优劣,而 ...

  4. java第二次试验报告

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1353       姓名:郭皓  学号:20135327 成绩:             指导 ...

  5. Spring笔记③--spring的命名空间

    p:命名空间: xmlns:p="http://www.springframework.org/schema/p" 作用:简化在xml配置bean的属性 在<bean> ...

  6. 软工1816 · Beta冲刺(7/7)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助完成安卓端的整合 完成安卓端的美化 协助制作宣传视频 接下来的计划 &am ...

  7. 学习总结:jQuery插件开发模式和结构

    学习博客链接: ①https://www.cnblogs.com/cyStyle/ ② https://www.cnblogs.com/chengyunshen/p/7277305.html ③ ht ...

  8. AVL树/线索二叉树

    此文转载: http://www.cnblogs.com/skywang12345/p/3577360.html AVL树是一棵特殊的高度平衡的二叉树,每个节点的两棵子树高度最大差为1.所以在每次的删 ...

  9. grep文本处理工具

    grep是一款文本过滤工具,基于正则表达式进行模式匹配sed:stream editor 流编辑器awk:linux上实现为gawk,文本报告生成器(格式化文本)文本搜索工具,根据用户指定的模式,对目 ...

  10. PAT 甲级 1141 PAT Ranking of Institutions

    https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184 After each PAT, the PA ...