草庐IT

new_string

全部标签

c++函数将time_t格式化为std::string:缓冲区长度?

我想要一个函数,它将接受一个time_t参数和一个任意格式的字符串并对其进行格式化。我想要这样的东西:std::stringGetTimeAsString(std::stringformatString,time_ttheTime){structtm*timeinfo;timeinfo=localtime(&theTime);charbuffer[100];strftime(buffer,100,formatString.c_str(),timeinfo);std::stringresult(buffer);returnresult;}但是我遇到的一个问题是缓冲区长度。我正在考虑做类似

c++ - operator new 负数

在C++11中,如果我们尝试使用全局运算符new分配负大小的数组,它会抛出std::bad_array_new_length,但是C++98/C++03呢?是UB还是会抛出std::bad_alloc?intmain(){int*ptr=newint[-1];} 最佳答案 如果大小为C++03标准的负数5.3.4p6,则程序不正确:Everyconstant-expressioninadirect-new-declaratorshallbeanintegralconstantexpression(5.19)andevaluateto

c++ - 使用 string.at() 或 string[]

在C++中,使用string[x]获取特定位置的字符是否不好?大多数人使用string.at(x)但string[x]不好的原因是什么? 最佳答案 据我所知,大多数人不使用string.at()。如果您的代码编写得很好并且易于理解,那么您应该始终在字符串的范围内工作,因此不需要string.at()提供的运行时边界检查。使用.at()的其他序列容器也是如此。 关于c++-使用string.at()或string[],我们在StackOverflow上找到一个类似的问题:

c++ - std::to_string 与字符串流

下面的代码显示了2个解决方案(std::to_string和std::stringstream)转换intm_currentSoundTime到std::string。std::to_string或std::stringstream更快吗?//ComputecurrentsoundtimeinminuteandconverttostringstringstreamcurrentTime;currentTime或m_currentSoundTimeInMinute=to_string(m_currentSoundTime/60); 最佳答案

c++ - 如何迭代 std::string 中的所有正则表达式匹配及其在 c++11 std::regex 中的起始位置?

我知道两种从std::string获取正则表达式匹配的方法,但我不知道如何获取所有匹配及其各自的偏移量。#include#include#includeintmain(){usingnamespacestd;strings="123apples456oranges789bananasorangesbananas";regexr=regex("[a-z]+");constsregex_token_iteratorend;//hereIknowhowtogetalloccurences//butdon'tknowhowtogetstartingoffsetofeachonefor(sreg

c++ - 在初始化内存上使用 placement new 是否合法?

我正在探索在C++中实现真正(部分)不可变数据结构的可能性。由于C++似乎不区分变量和变量存储的对象,因此真正替换对象(无需赋值操作!)的唯一方法是使用placementnew:autovar=Immutable(state0);//thefollowingisillegalasitrequiresassignmentto//animmutableobjectvar=Immutable(state1);//however,thefollowingwouldworkasitconstructsanewobject//inplaceoftheoldonenew(&var)Immutable

c++ - 有什么方法可以在不复制的情况下从 std::ostringstream 获取 std::string_view ?

我知道你不能在不复制的情况下从std::ostringstream中提取std::string(Creatinganinputstreamfromconstantmemory)。但是有可能得到一个std::string_view吗? 最佳答案 字符串流不需要将它们的数据存储在一个连续的数组中。string_view当然是连续字符串的View。所以不,你想要的是不可能的。最好等到C++20,那时我们将支持移入/移出字符串流。 关于c++-有什么方法可以在不复制的情况下从std::ostri

c++ - 使用 std::string 在 switch-case block 中返回字符串常量

注意:这不是关于使用字符串来选择switch-caseblock中的执行路径。C++中的一个常见模式是使用switch-caseblock将整数常量转换为字符串。这看起来像:charconst*to_string(codescode){switch(code){casecodes::foo:return"foo";casecodes::bar:return"bar";}}但是,我们是在C++中,所以使用std::string更合适:std::stringto_string(codescode){switch(code){casecodes::foo:return"foo";caseco

c++ - std::string 删除最后一个字符失败?

我正在尝试将通配符形式的用户输入("*word*")更改为正则表达式格式。为此,我使用下面的代码去除输入开头和结尾的'*',以便我可以在两端添加正则表达式字符:string::iteratoriter_begin=expressionBuilder.begin();string::iteratoriter_end=expressionBuilder.end();iter_end--;if((char)*iter_begin=='*'&&(char)*iter_end=='*'){expressionBuilder.erase(iter_begin);expressionBuilder.

c++ - operator new 如何调用类的构造函数?

我知道,new操作符会调用类的构造函数。但是它是如何发生的,用于此的底层技术是什么。 最佳答案 这是我想象的样子:T*the_new_operator(args){void*memory=operatornew(sizeof(T));T*object;try{object=new(memory)T(args);//(*)}catch(...){operatordelete(memory);throw;}returnobject;}(*)从技术上讲,它并不是真正调用placement-new,但只要你不重载它,心智模型就可以正常工作: