草庐IT

list_type

全部标签

c++ - 删除 std::list::iterator 不会使迭代器无效并销毁对象吗?

为什么下面打印2?listl;l.push_back(1);l.push_back(2);l.push_back(3);list::iteratori=l.begin();i++;l.erase(i);cout我知道erase返回什么,但我想知道为什么这样可以?或者它是未定义的,还是取决于编译器? 最佳答案 是的,这是未定义的行为。您正在取消引用一种野指针。在erase之后,您不应该使用i的值。是的,erasedestructs指向的对象。但是,对于POD类型,销毁不会执行任何操作。erase不会为被删除的迭代器分配任何特殊的“空”

c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?

我的代码有问题。在Xcode或使用C++11编译器中,此代码运行良好。但是,当我将此代码提交给在线法官时,判决显示“编译错误”。我认为他们使用的是C++4.7.1编译器,当我尝试编译它(使用Ideone)时,它说:prog.cpp:Infunction'voidprintArray(int)':prog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-ob

c++ - "missing type specifier"构造函数声明错误

我在2个不同的文件中有2个类:正则矩阵.h:#ifndef_RM_H#define_RM_H#include"SparseMatrix.h"...classRegMatrix{...RegMatrix(constSparseMatrix&s){...}//ctor...};#endif稀疏矩阵.h:#ifndef_SM_H#define_SM_H#include"RegMatrix.h"...classSparseMatrix{...SparseMatrix(constRegMatrix&r){...}//ctor...};#endif在构造函数行上我得到了错误:错误C4430:缺少类

c++ - 错误 :incompatible types in assignment of 'const char[5]' to 'char[10]'

我已经将c定义为charc[][10]在函数定义中并像c[i]="gray";一样使用它怎么了?我在网上搜索,它显示相同的语法。谢谢。 最佳答案 您不能对数组使用赋值(=)。如果将c更改为指针数组,这可能会起作用,具体取决于您需要使用它做什么。constchar*c[20];c[i]="gray";或者如果声明的类型必须是数组的数组,您可以使用strncpy:charc[20][10];strncpy(c[i],"gray",sizeof(c[i])); 关于c++-错误:incompa

c++ - std::shuffle 不能用 std::list 编译

我正在尝试打乱一些生成元素的列表。这是代码:std::default_random_enginegenerator(10);std::listlist(10);intn=0;std::generate(list.begin(),list.end(),[&]{returnn++;});std::shuffle(list.begin(),list.end(),generator);它不编译。以下是错误:/include/c++/v1/algorithm:3059:34:Invalidoperandstobinaryexpression('std::__1::__list_iterator'

C++ list<T>::iterator 不能在派生类模板中使用

g++编译器给出了这个错误:expected`;'在“它”之前templateclassmyList:publicstd::list{public:voidfoo(){std::list::iteratorit;//compilererrorasabovementioned,why???}};谢谢。 最佳答案 在g++中。每当在模板中看到错误时:error:expected';'before'it'怀疑你需要一个类型名:typenamestd::list::iteratorit;当您在模板中声明了一个依赖于一个或多个模板参数的新类型

c++ - 从 std::list 中移除具有特定值的元素

我需要从std::list中删除具有特定值的元素。随着list我使用了remove()方法。现在我有list所以我想我应该使用remove_if()但它的谓词只需要一个参数-要测试的元素。如何编写函数foo(constCMyClass&Bad)从列表中删除所有等于Bad的元素?谢谢附言structCMyClass{void*Ptr;intVar;}boolis_equal(constCMyClass&A,constCMyClass&B){if(A.Ptr==B.PrtandA.Var==B.Var)returntrue;elsereturnfalse;} 最

c++ - 错误 : Variable length array of Non-POD element type 'string'

在开始之前,我必须首先声明,我已经研究过针对此错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我项目的要求。另外,我目前正在学习CS入门类(class),所以我的经验几乎没有。数组的用途是从文件中收集名称。因此,为了初始化数组,我计算了名称的数量并将其用作大小。问题是标题中所述的错误,但我仍然使用一维数组时看不到解决方法。主要.cpp#include#include#include#include#include#include"HomeworkGradeAnalysis.h"usingnamespacestd;intmain(){ifstreaminfile;ofstrea

c++ - 如何通过 C++ 可执行文件中的 list 启用 "Long Path Aware"行为?

我正在尝试关注Microsoftdocumentation解除Windows10下API中的MAX_PATH文件路径限制。它说:Youcanalsoenablethenewlongpathbehaviorperappviathemanifest:true所以,第一个问题。是否可以在VisualStudio2017的项目属性中启用它?第二个问题:我没有找到上面的答案,所以我决定走手动路线:我创建了additional.manifest文本文件:true然后我将它添加到项目属性中:但是当我编译它时,它给了我这个警告,并且该list在应用程序运行时似乎没有任何效果:1>additional.

c++ - 在 C++14 中将 constexpr intializer_list 作为参数传递

为什么这行不通:constexprinitializer_listilist={1,2,3,4};constexprintmy_min=min(ilist);虽然这样做:constexprintmy_min=min({1,2,3,4});我的代码基于constexprstd::min()函数,如图所示here我正在使用clang3.5.0编译器(g++4.9.1似乎不知道constexprstd::min())。我无法理解我遇到的错误:clang35-stdlib=libc++-std=c++14test.cpp-otest;test.cpp:158:35:error:constexp