哪个字符串类在c++中使用高性能和简单
which string class to use in high performance and to be simple in c++
我正在用c++构建一个简单的框架,我听说std::string类在关键性能情况下不好,我知道在c++11中由右值引用的move构造函数修复的返回时复制问题。
-
C++11解决了这些问题吗??如果是,那么为什么有string_ref的提议http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3334.html
-
是否建议使用std::string而不是任何其他字符串?
- 同样,那些习惯于处理C#的用户使用std::string的简单性是非常困难的,我决定为我的字符串提供类似于.NET中System::string中的方法(我的意思是相同的名称,相同的场景,因为我知道std和STL提供了我需要的所有功能)如果字符串的性能很好,那么将其封装在一个提供所需功能的自定义类中的想法有多糟糕?如下所示:
//this is a simple scratch for what I meant by wrapping std::string inside a custom string
class CustomString
{
public:
CustomString()// : str(nullptr) //shared_ptr will initialize to zero
{
}
CustomString(const char* str)
{
str = make_shared<std::string>(str);
}
uint32_t IndexOf(char c)
{
//call the appropriate methods in the str->find(...
}
uint32_t IndexOf(const char* ofStr)
{
//call the appropriate methods in the str->find(...
}
uint32_t IndexOf(const CustomString& ofStr)
{
//call the appropriate methods in the str->find(...
}
CustomString SubString(uint32_t start = 0, uint32_t length = -1)
{
//call the appropriate methods in the str->substr(...
}
CustomString LastIndexOf(const CustonString& str)
{
//call the appropriate methods in the str->rfind(...
}
//............
//.......
//..complete all other needed functionality
//.does the added in-between method call will have a noticeable effect on the performance
private:
shared_ptr<std::string> str;
};
回答您的一个问题:
是否建议使用
std::string
而不是任何其他字符串?
是的。如果其他人想使用你的框架,那么如果你使用std::string
,他们就不必担心学习使用新的字符串类。在大多数情况下,std::string
的性能是令人满意的。
编辑:
当您不想使用std::string
时,是指您想传递一个字符串让其他代码查看,但不想触摸。注意,与.NET中的字符串不同,c++中的字符串是可变的。因此,在c++中,按值传递std::string
s的代价要高得多(因为有副本),所以您希望使用const std::string&
。
另一种不想使用std::string
的情况是,当您想编写一个采用字符串文字的函数时(即,您需要一个可以查看但不能编辑的硬编码字符串)。为此,您需要使用const char *
。这是你所能达到的效率。此外,另一个c++编程人员在查看您的代码并看到const char *
时,会立即想到"字符串文字",这使代码更易于解释。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 哪个字符串类在c++中使用高性能和简单