`

std::string 的两种实现方式

 
阅读更多

有两种:

1. COW(copy on wirte):

是当前主流的实现,不过在多线程环境下会有性能问题

参考:

How is std::string implemented?

http://stackoverflow.com/questions/1466073/how-is-stdstring-implemented

 

Copy-On-Write的原理及具体实现


Copy On Write(写时复制)

2.Short string optimization

为解决cow暴露的问题而出现另一种实现方案,llvm子项目“新libc++ 0x"采用这种实现。

 

From years of experience (including having implemented the standard library before), we've learned many things about implementing the standard containers which require ABI breakage and fundamental changes to how they are implemented. For example, it is generally accepted that building std::string using the "short string optimization" instead of using Copy On Write (COW) is a superior approach for multicore machines (particularly in C++'0x, which has rvalue references). Breaking ABI compatibility with old versions of the library was determined to be critical to achieving the performance goals of libc++.
 

参考

libc++ 0x 项目

http://libcxx.llvm.org/

分享到:
评论

相关推荐

    std::string format格式化函数源代码及两种格式化方法

    本程序提供了std::string 类型的Format格式化函数,以及两种格式化string字符串的方法,主方法在str.hpp文件中,测试文件在string_format.cpp中,已测试可用

    利用C++实现从std::string类型到bool型的转换

    利用输入字符串流:std::istringstream 代码如下:...不足:除了加条件判断语句外,暂未找到其它能同时涵盖两种情况的简单方法。 您可能感兴趣的文章:详解C++中String类模拟实现以及深拷贝浅拷贝自己模拟写C++中的St

    详解C++ string常用截取字符串方法

    string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子...

    定长字符串模板类

    针对char和wchar_t(我特讨厌这后面的_t这根尾巴)两种字符类型的基类的和默认traits 的typedef.写函数参数类型的时候可以少写几个字, ^=^ 4. template <;size_t MaxSize, typename Traits=std::char_...

    C++智能指针原理.pdf

    ⾸先看2个函数: //函数1 void remodel(std::string & str) { std::string * ps = new std::string(str); ... str = ps; return; } //函数2 void remodel(std::string &str) { std::string * ps = new std::string...

    beef:更快,更紧凑的std实现

    牛肉 Cow更快,更紧凑的实现。 --- use beef :: Cow; let borrowed: Cow < str> = Cow :: borrowed ( "Hello" ); let owned: Cow < str> =...两种版本都比std::borrow::Cow精简: use std :: mem :: size_of; const W

    基于boost的序列化与反序列化

    1、包含文件和二进制两种序列化的方式 2、支持类的序列化和STL容器的序列化 3、支持序列化的流自定义(示例中使用的是std::stringstream,也可以使用其他的流)

    LuaBind 源码 (Lua增强库)

    void print_string() { std::cout << m_string ; } private: std::string m_string; }; 为了注册这个类到Lua环境,可以像下面这样写(假设你使用了名空间): module(L) [ class_("testclass") .def(constructor...

    浅析string类字符串和C风格字符串之间的区别

    C风格字符串和char数组是不一样的,看下面两种定义:char carr1 = {‘a’, ‘b’, ‘c’};char carr2 = {‘a’, ‘b’, ‘c’, ‘\0’};看上面,carr2可以说成是C风格字符串,carr1就不是C风格字符串,C风格字符串...

    C++ 中String 替换指定字符串的实例详解

    C++ 中String 替换指定字符串的...可以是22211,也可以是21221,有时候应用的场景不同,就会希望得到不同的结果,所以这两种答案都做了实现, 代码如下: #include <string> #include using namespace std; strin

    C++实现56dxw短信验证码WebService接口--

    56短信网有两种接口,一是HTTP,二是 Web Service。 短信验证码,采用HTTP接口,在客户端发送是不实际的,因为一是受制于客户端网络稳定性,二是容易被客户端拦截URL, 三是被拦截后,容易泄漏56短信网的帐号密码...

    Tinyxml 源代码(VC6 & VS2005)

    TinyXML一开始就两种方式都支持。调用TiXmlBase::SetCondenseWhiteSpace( bool )来设置你想要的结果,默认是压缩掉多余的空格。 如果想要改变默认行为,你应该在解析任何XML数据之前调用TiXmlBase::...

    C++中string替换所有指定字符串的方法

    可以是22211,也可以是21221,有时候应用的场景不同,就会希望得到不同的结果,所以这两种答案都做了实现,代码如下: # include # include using namespace std; string& replace_all(string& str, const string& ...

    C++生成格式化的标准字符串实例代码

    众所周知,C++的std::string功能残缺,各种功能都没有,比如格式化字符串功能。 在python3中,支持两种格式化字符串的方法,一种是C风格,格式化的部分用%开头,%后面的对应具体类型(比如%s对应字符串%d对应整型)...

    QString的常用方法(小结)

    C++原生提供两种字符串: 传统的C风格以’\0’结尾的字符数组和std::string类。与这些不同,QString使用16-bit Unicode值 当使用QString时,我们不必操心如此隐秘的细节,像分配足够的内存或者是数据是’\0’结尾的...

    C++17 Standard Library Quick Reference, 2nd Edition.pdf

    此快速参考是对C++17标准库提供的基本数据结构、算法和功能的...这个新版本被更新以包括C17中的所有标准库更改,包括新的词汇表类型std:String_view、Any、可选和变体;并行算法;文件系统库;专门的数学函数;等等。

    C++标准库中sstream与strstream的区别详细解析

    在C++有两种字符串流,一种在sstream中定义,另一种在strstream中定义。它们实现的东西基本一样。 strstream里包含class strstreambuf;class istrstream;class ostrstream;class strstream;它们是基于C类型字符串...

    c++ 面试题 总结

    6.下面是C语言中两种if语句判断方式。请问哪种写法更好?为什么? int n; if (n == 10) // 第一种判断方式 if (10 == n) // 第二种判断方式 如果少了个=号,编译时就会报错,减少了出错的可能行,可以检测出是否少...

    最新名企标准通用C++面试题,

    (请至少说明两种) 答: (1)可以定义 const 常量 (2)const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。 2、在C++ 程序中调用...

    C++输入一个字符串,把其中的字符按照逆序输出的两种方法解析

    用字符数组方法:基本思路是,先判断字符...实现代码: 代码如下:#include<iostream>using namespace std;int main(){ char a[50]; cout<<“please input a string:”; cin>>a; int i=0,k=0; while(i; >=0

Global site tag (gtag.js) - Google Analytics