草庐IT

overloaded-strings

全部标签

c++ - 以安全的方式从 char* 创建 std::string

我有一个char*p,它指向一个以\0结尾的字符串。如何以异常安全的方式从中创建C++string?这是一个不安全的版本:stringfoo(){char*p=get_string();stringstr(p);free(p);returnstr;}一个明显的解决方案是try-catch-有更简单的方法吗? 最佳答案 您可以使用C++11中的shared_ptr或Boost:stringfoo(){shared_ptrp(get_string(),&free);stringstr(p.get());returnstr;}这使用了sh

c++ - 将 uint64_t 转换为 std::string

如何将uint64_t值传输到std::string?我需要构造包含此值的std::string例如这样的事情:voidgenString(uint64_tval){std::stringstr;//.....somecodeforstrstr+=(unsignedint)val;//????}谢谢 最佳答案 在C++11中你可以只使用:std::to_string()它在标题中定义http://www.cplusplus.com/reference/string/to_string/

c++ - 为什么将 std::string 写入 cout 会导致未知运算符 << 错误?

当我尝试从我的方法之一输出返回值时出现错误:Error:Nooperator"main.cpp#includeusingnamespacestd;#include"Book.h"intmain(){book.setTitle("AdvancedC++Programming");book.setAuthorName("Linda","Smith");book.setPublisher("MicrosoftPress","OneMicrosoftWay","Redmond");book.setPrice(49.99);cout>i;return0;};返回字符串的方法:stringBook

c++ - std::string::c_str() 是否总是返回以 null 结尾的字符串?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:stringc_str()vs.data()我使用strncpy(dest,src_string,32)将std::string转换为char[32]来创建我的C++类使用遗留C代码。但是std::string的c_str()方法是否总是返回以null结尾的字符串?

没有发现能够从类型[java.lang.string]转换为[org.springframework.data.data.solr.core.geo.geo.point]的转换器。

我正在尝试使用Spring-Data-Solr,以通过我的SpringBoot应用程序访问Solr实例。我有以下bean类:@SolrDocument(solrCoreName="associations")publicclassAssociationimplementsPlusimpleEntityI{@Id@IndexedprivateStringid;@IndexedprivateStringname;@IndexedprivatePointlocation;@IndexedprivateStringdescription;@IndexedprivateSettags;@Indexedp

c++ - 使用 C 样式字符串文字与构造未命名的 std::string 对象的默认建议?

因此,C++14引入了许多要使用的用户定义文字,其中之一是"s"literalsuffix,用于创建std::string对象。根据文档,其行为与构建std::string完全相同。对象,像这样:autostr="HelloWorld!"s;//RHSisequivalentto:std::string{"HelloWorld!"}当然构建一个未命名的std::string对象可以在C++14之前完成,但是因为C++14的方式要简单得多,我认为更多的人实际上会考虑构建std::string现场的物体比以前多,这就是为什么我认为提出这个问题是有道理的。所以我的问题很简单:在什么情况下,构

当我尝试将base64string转换为C#中的图像时

当我尝试将base64string转换为C#中的图像时,我将输出作为“System.Drawing.bitMap”而不是实际映像:publicImageDownFile(stringbase64String)//stringfile{//ConvertBase64Stringtobyte[]byte[]imageBytes=Convert.FromBase64String(base64String);MemoryStreamms=newMemoryStream(imageBytes,0,imageBytes.Length);//Convertbyte[]toImagems.Write(imag

C++ 的 std::string 池,调试版本? std::string 和 valgrind 问题

我遇到许多关于std::string中可能的内存泄漏的valgrind警告问题,比如这个:120bytesin4blocksarepossiblylostinlossrecord4,192of4,687at0x4A06819:operatornew(unsignedlong)(vg_replace_malloc.c:230)by0x383B89B8B0:std::string::_Rep::_S_create(unsignedlong,unsignedlong,std::allocatorconst&)(in/usr/lib64/libstdc++.so.6.0.8)by0x383B8

c++ - string::c_str() 是否允许在堆上分配任何东西?

如果我需要从std::string中获取一个以NUL结尾的char数组,在这种情况下我需要确保不会分配任何内容,是使用c_str这样做安全吗?例如,如果我在析构函数中并且我想将一些数据从string复制到一个预先分配的固定大小的缓冲区中,我可以使用c_str和确定它不会抛出任何东西吗? 最佳答案 标准规定调用c_str()可能会使引用string元素的引用、指针和交互器无效,这意味着允许重新定位(21.3/5"类模板basic_string").您可能只想调用string::copy()来获取拷贝(如果需要,您需要自己添加空终止符)

c++ - 返回 char 数组作为 std :string

如果没有显式转换或调用std::string构造函数,以下内容是否安全?如果不安全,为什么不呢?std:stringmyfunc(){charbuf[128]="";//putsomethingintobufornotbaseonlogic.returnbuf;} 最佳答案 是的。那很好。调用者将获得本地缓冲区的拷贝,因为std::string会从该本地缓冲区中进行深度复制!编辑:我假设buf是空终止字符串! 关于c++-返回char数组作为std:string,我们在StackOver