草庐IT

string.format

全部标签

c++ - std::map 中的 std::string 导致 Valgrind 内存泄漏

这是Valgrind的输出:==6519==at0x4C25885:operatornew(unsignedlong)(vg_replace_malloc.c:319)==6519==by0x4EE65D8:std::string::_Rep::_S_create(unsignedlong,unsignedlong,std::allocatorconst&)(new_allocator.h:104)==6519==by0x4EE7CE0:char*std::string::_S_construct(charconst*,charconst*,std::allocatorconst&,s

c++ - 复制初始化期间不会发生 std::string 的隐式构造

我正尝试在main()函数中复制初始化我的CObj类:#include#includeclassCObj{public:CObj(std::stringconst&str):m_str(str){std::cout但是,即使std::string是从charconst*隐式构造的,CObjobj="hello"行也无法编译>。根据我在这里的理解,这应该有效。有什么理由不这样做吗?如果我这样做,它会起作用:CObjobj=std::string("hello"); 最佳答案 文字"Hello"的类型为constchar[6]:为了调用

c++ - Lint 更喜欢 std :string rather than clear() 上的 erase() 方法

使用Lint,它反复反对在std字符串上使用clear,并希望建议使用不带参数的删除方法。查看文档并执行我想要的操作,即将字符串的大小调整为零个元素,以便该字符串为空且没有任何元素。此时我不想删除字符串,只需从中删除所有成员即可。我的问题是这两种方法有什么区别,有谁知道我应该使用erase而不是clear方法的原因吗? 最佳答案 我看不出任何原因。恰恰相反:我实际上不得不查看引用以查看erase()是否可以在没有参数的情况下调用。erase()用于从字符串中删除一些指定的字符。clear()用于删除字符串的全部内容(但不包括其容量)

c++ - 如何干净地使用:const char* 和 std::string?

tl:drHowcanIconcatenateconstchar*withstd::string,neatlyandelegantly,withoutmultiplefunctioncalls.Ideallyinonefunctioncallandhavetheoutputbeaconstchar*.Isthisimpossible,whatisanoptimumsolution?初始问题到目前为止,我在C++中遇到的最大障碍是它如何处理字符串。在我看来,在所有广泛使用的语言中,它处理字符串的能力最差。我见过其他与此类似的问题,这些问题的答案要么是“使用std::string”,要么只

c++ - 如何连接 "constant strings"和字符?

执行以下操作的“正确方法”是什么?(注意,我还不想将消息输出到屏幕上,数据需要存储在一个变量中。)std::cout>input;std::stringmessage="Today'sprogramwasbroughttoyoubytheletter'"+input+"'.";代码给出了错误消息invalidoperandsoftypesconstchar*andconstchar[3]tobinaryoperator+。我明白为什么会出现此消息。在谷歌搜索解决方案时,出现的结果建议依次将每个项目转换为字符串。但是,如果您必须连接十几个项目,这就变得不切实际了:std::string(

C++ 从 char* 到 char 的无效转换 (char* = *string.begin() )

我有以下代码:std::stringextract(){fstreamopenfile("/home/name/Documents/testfile");std::stringteststring;longlocation=4;longlength=2;teststring.resize(length);char*begin=*teststring.begin();openfile.seekp(location);openfile.read(begin,length);returnteststring;}此代码应该返回在文件中找到的字符串。例如,如果文件的内容是StackOverflo

c++ - "Why switch statement cannot be applied on strings?"的答案是否仍然正确,即使使用 C++11/14?

我遇到了这个问题:Whyswitchstatementcannotbeappliedonstrings?并想知道答案是否:Thereasonwhyhastodowiththetypesystem.C/C++doesn'treallysupportstringsasatype.Itdoessupporttheideaofaconstantchararraybutitdoesn'treallyfullyunderstandthenotionofastring.仍然适用,即使在C++11/14中使用std:string。是否有多个elseif(...)的替代方案?

c++ - boost::shared_ptr<string> 标准集

我有一组boost::shared_ptr,我希望它不是通过共享指针而是通过字符串来排序和唯一化的。我是否必须提供一个新的比较函数来获取共享指针并比较内容,或者已经存在我可以使用的比较器? 最佳答案 这非常具体,因此您可能需要一个自定义比较器。这应该有效:structpointercompare{booloperator()(constboost::shared_ptr&a,constboost::shared_ptr&b){return(*a)>(*b);}} 关于c++-boost:

c++ - 为什么 c++ string == (equality) 运算符比手动逐个检查字符快得多?

我在玩C++字符串,发现使用C++字符串==运算符比手动逐个检查字符要快得多:#include#include#includeusingnamespacestd;//assumess1ands2areofsamelengthboolmyEqual(string&s1,string&s2){inti=0;intj=0;while(i输出显示:MyEqual:18==operator:3对于较大的字符串,差异更为显着。我最初认为c++string==operator会做一些与手动逐个比较字符非常相似的事情,但显然它使用了一些优化来显着优于手动方法。c++string==操作符做了哪些优化?

c++ - 从相同的硬编码字符串文字初始化 std::string 和 std::wstring

我正在编写一些单元测试时偶然发现了一个已经成功困扰我几次的场景。我需要生成一些字符串来测试JSON编写器对象。由于作者同时支持UTF16和UTF8输入,所以我想用这两种输入进行测试。考虑以下测试:classUTF8;classUTF16;templatevoidwriteJson(std::map&data){//Writetofile}voidgenerateStringData(std::map&data){data.emplace("Lorem","LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.");