草庐IT

c++ - 模板化放置新的和析构函数

为什么不能编译?templateclassPool{charBuff[sizeof(T)*256];public:Pool(){T*item=reinterpret_cast(&Buff[0]);for(inti=0;i(&Buff[0]);for(inti=0;i~T();}voidreset(unsignedinti){T*item=reinterpret_cast(&Buff[0]);item[i]->~T();item[i]->T();}}我显然想要实现的是在原始内存数组上调用placementnew(应该调用构造函数ok)。然后我想调用数组中项目的析构函数和构造函数。问题是I

c++ - 如何使用 ctypes 从 C++ 函数返回对象?

我有两个C++类,Container和Item,它们看起来像:classItem{public:Item(std::string*name,intid);std::string*getName();private:std::string*name;intid;};classContainer{public:Container();Item*getItem(intid);private:std::vectoritems;};我想在Python中创建和使用Container,所以我写了一个C接口(interface)来编译一个共享库:extern"C"{Container*Containe

C++ 使用声明与枚举 : how to import all the enum items?

这是演示我的问题的代码片段。namespaceN{enumE{A,B,C,D};}intmain(){usingN::E;Ee=A;//syntaxerror:'A'isnotdeclared}最后一行给我一个语法错误。我想使用名称N::A、N::B、N::C和N::D在没有命名空间限定符N::的主函数中。但是我不想做以下两件事(1)我不想说usingnamespaceN,因为那样会在N中导入其他一切。(2)我不想为枚举的每个成员说usingN::A、usingN::B等。因为如果我想修改枚举,我也必须更改我的主要功能。更不用说额外的输入既乏味又容易出错。我尝试自己寻找答案,但找不到。

c++ - 为什么将字符串放入结构中会导致崩溃?

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭10年前。我有一个结构,其中包含几个字符串。structitem{stringitem_name;intitem_property_1;doubleitem_property_2;}稍后我初始化它们:itemitem1;item1.item_name="NameofItem";item1.item_property_1=5;item1.item_property_

c++ - 模板对象可以改变它的类型吗?

我有以下类(class)templateclassItem{public:Titem;Item():item(T()){}Item(Targ){this->item=arg;}operatorT()const{returnitem;}};现在我想写一个赋值运算符,它也改变对象的类型。这可能吗?我用谷歌搜索了它,但没有得出任何相关结果(顺便说一句,这让我觉得我可能有点疯了)。为了清楚起见,假设我有以下2个对象:ItemintItem=3;ItemdoubleItem=3.4;我想写intItem=doubleItem;然后,我希望intItem的类型为Item.如果我只想要一个“经典的”

c++ - 检查构造函数参数

classitem{inti;public:item(intno){}};我想检查构造函数参数。如果发现它持有负值,则应停止对象创建。这里不能使用异常,因为目标系统不支持异常。 最佳答案 没有抛出就无法停止对象的创建。您可以做的最好的事情是设置一个“无效参数”标志,您必须在之后检查该标志,如果为真,则丢弃该对象而不使用它。根据您的要求,使用工厂方法创建对象可能会更好——这样,您可以在调用构造函数之前进行检查:classitem{inti;public:staticitem*create(intno){if(no你可以这样使用item

c++ - Qt:在 TreeView 中设置列

我如何为qTreeView实现qTreeWidget的代码?for(constauto&i:names){QTreeWidgetItem*item=newQTreeWidgetItem(ui->treeWidget);item->setText(0,QString::fromStdString(i));ui->treeWidget->addTopLevelItem(item);conststd::unordered_mapmap=m_reader.getMapFromEntry(i);for(constauto&j:map){QTreeWidgetItem*item2=newQTree

C++ 模板和指针

我对模板和指针有疑问(我认为)。以下是我的部分代码:/*ItemCollection.h*/#ifndefITEMCOLLECTION_H#defineITEMCOLLECTION_H#includeusingnamespacestd;templateclassItemCollection{public://constructor//destructorvoidinsertItem(constT);private:structItem{Tprice;Item*left;Item*right;};Item*root;Item*insert(T,Item*);};#endif以及带有函数定

c++ - 从不可排序的 vector 中删除重复项

我正在寻找一种从vector中删除重复项的方法(让我们称他为theGreatVector:D)。我不能使用std::sort后跟std::unique,因为无法对我的对象进行排序。theGreatVector包含一些vector(小vector)我为vector重载了==所以我可以使用它我能够在O(n²)内创建一些东西,但我需要时间效率(theGreatVector.size()可以是10⁵或10⁶)现在我得到的是类似的东西(只有当smallOne不在其中时,我才填充我的vector):for(i=0;ismallOne=FindFacets(i)if(smallOnedoesntbe

c++ - 在 hashmap/unordered_map 中,当 value 已经包含 key 时,是否可以避免数据重复

给定以下代码:structItem{std::stringname;intsomeInt;stringsomeString;Item(conststd::string&aName):name(aName){}};std::unordered_mapitems;Item*item=newItem("testitem");items.insert(make_pair(item.name,item);项目名称将在内存中存储两次-一次作为项目结构的一部分,一次作为map条目的键。是否可以避免重复?对于大约100M的记录,这种开销变得巨大。注意:我需要在Item结构中包含名称,因为我使用hash