草庐IT

new_string

全部标签

c++ - typedef a std::string - 最佳实践

我正在用标准C++编写一个库来进行语音转换。到目前为止,我已经使用了std::string。但将来我可能不得不将其更改为其他内容(std::wstring或其他内容)。所以我需要以一种可以轻松切换的方式编写我的库。到目前为止,我已经完成了以下工作。创建了一个将被所有CPP文件使用的头文件为此添加了“typedefstd::string”,并在文件中各处使用了新名称。如果我需要改变类型,我可以简单地在头文件中改变,它会在所有地方反射(reflect)出来。如果有人能看出这是正确的方法或者有更好的方法来做到这一点,我将不胜感激?谢谢 最佳答案

c++ - STL 分配器和运算符 new[]

是否有使用operatornew[]的STL实现?作为分配器?在我的编译器上,生成Foo::operatornew[]private并没有阻止我创建vector...这种行为有任何保证吗? 最佳答案 C++标准,第20.4.1.1节。默认分配器allocate()函数使用全局运算符new:pointerallocate(size_typen,allocator::const_pointerhint=0);3Notes:Uses::operatornew(size_t)(18.4.1).

c++ - 如何使用 boost.spirit 提取 std::string?

我尝试使用boost.spirit解析command:param1param2...形式的简单命令行为此我创建了这个解析器:(+(char_-':'))[ref(cmd)=_1]>>':'>>(*char_)[ref(params)=_1]这两个复合解析器的属性类型都是vector,所以如果cmd和params是vector类型就可以了。但是,如果它们是std::string类型,则不会。在网上搜索此解决方案时,我发现提示它也适用于字符串。无论如何我可以用字符串来完成这项工作吗? 最佳答案 当然,当您使用语义操作时,不会发生自动属性

c++ - 为什么 std::cin.getline 没有重载方法来获取 std::string?

我很好奇cin.getline的技术原因和globalgetline功能在不同的地方。不简单地为cin定义所有这些函数签名的动机是什么://THESETWOEXISTistream&cin::getline(char*s,streamsizen);istream&cin::getline(char*s,streamsizen,chardelim);//THESETWOCOULDEXISTistream&cin::getline(string&s);istream&cin::getline(string&s,chardelim);是不是因为可能要添加其他类型,不想把字符串嫁给cin?

c++ - 将 raw operator new、placement new 和 standard delete 结合起来是否合法?

伙计们!出于好奇——以下代码可能不合法,对吗?T*p=::operatornew(sizeof(T));//allocatememoryforaTnew(p)T;//constructaTintotheallocatedmemorydeletep;//deletetheobjectusingthestandarddeleteoperator 最佳答案 没有。您只能删除从新返回的内容-没有异常(exception)。 关于c++-将rawoperatornew、placementnew和s

c++ - 防止继承 operator new 和 delete

(我确实扫描了,但找不到任何类似的东西,如果有欺骗请关闭)。有没有办法防止这两个运算符被继承?例如:structfoo{staticvoid*operatornew(std::size_t){//special}staticvoidoperatordelete(void*p,std::size_t){//special}};structbar:publicfoo{};现在bar将继承这两个运算符-在这种微不足道的情况下,没什么大不了的,如果foo和bar中有数据成员,就会出现问题(在我的情况下更糟,因为foo的分配需要与bar不同!)现在避免这种情况的方法是在bar,我也会实现操作符。

c++ - STL String::length() 段错误

#include#include#include/*UsingSTL'sstringclassbecausetheproblemdoesnotreferanylimitsregardingthenumberofcharactersperline.*/usingnamespacestd;intmain(){stringline;vectorlines;while(getline(cin,line)){lines.push_back(line);}unsignedinti,u;unsignedintopening=1;//2iflastwasopening,1ifitwasclosingf

c++ - 在 vector<vector<string>> 上使用 push_back

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭9年前。这么一个简单的问题让我陷入困境,我感到有些尴尬,但经过几个小时的谷歌搜索毫无结果后,我仍然被困住了。为了简化我的问题,第二行崩溃了:vector>sorted_words;sorted_words[0].push_back("hello");sorted_words[0]不应该代表一个我可以合法push_back的空vector吗?

c++ - std::string s() 奇怪的行为

这个问题在这里已经有了答案:Whyistherenocalltotheconstructor?[duplicate](3个答案)关闭9年前。我发现了一些我不明白的奇怪东西。std::stringa();打印出来时返回1。我不知道它是从哪里来的。我认为a()是一个没有参数的构造函数,但看起来它不是。我在哪里可以找到这方面的信息?这是什么?当尝试执行std::stringb(a);时,编译器会提示:error:nomatchingfunctionforcallto‘std::basic_string::basic_string(std::string(&)())’解释将不胜感激。

c++ - 为什么输出带有转换运算符的类不适用于 std::string?

Thisworks,printing1:#includestructInt{inti;operatorint()constnoexcept{returni;}};intmain(){Inti;i.i=1;std::cout然而,thisfailstocompileonGCC4.8.1:#include#includestructString{std::strings;operatorstd::string()const{returns;}};intmain(){Strings;s.s="hi";std::cout以下是错误的相关部分:error:nomatchfor‘operators