草庐IT

Initializer-list

全部标签

c++ - 避免 std::list 中的指针

我尽量避免有指针,而不是做std::list*>myList;voidaddElement(inta,intb){myList.push_back(newstd::pair(a,b));}我想我可以做类似的事情std::list>myList;voidaddElement(inta,intb){std::pairp(a,b);myList.push_back(p);}如果我对行为的理解正确,这应该存储对的拷贝,并在执行myList.clear()时自动删除它(与指针相反)。这是最好的方法吗?我可以期望编译器优化掉不必要的对象p吗? 最佳答案

c++ - std::list<>:l.begin() 之前的元素

简短的问题:使用与我不同的其他编译器(mingw32),以下代码是否不安全,或者是否可以使用?listl;/*addelements*/list::iteratori=l.begin();i--;i++;cout...或者换句话说:i是否定义为指向此之后的l.begin()? 最佳答案 是的,代码是不安全的。一旦您尝试在begin()之前移动,您就会导致未定义的行为。尝试“再次返回”可能行不通。 关于c++-std::list:l.begin()之前的元素,我们在StackOverflo

c++ - 加速 C++ : Can I write a program that sorts either a list or a vector using the same command?

我意识到std::sort函数需要使用随机访问迭代器,而列表具有双向迭代器。有一个关于此的问题:SortlistusingSTLsortfunction我正在努力回答AcceleratedC++书中的问题5-4以供家庭学习。5-4.Lookagainatthedriverfunctionsyouwroteinthepreviousexercise.Notethatitispossibletowriteadriverthatonlydiffersinthedeclarationofthetypeforthedatastructurethatholdstheinputfile.Ifyour

C++11 统一初始化 : Field initializer is not constant

我正在尝试像这样实例化一组字符串:classPOI{public:...staticconststd::setTYPES{"restaurant","education","financial","health","culture","other"};...}现在,当我这样做时,我得到了这些错误(全部在这一行):error:fieldinitializerisnotconstantstaticconststd::setTYPES{"restaurant","education","financial","health","culture","other"};error:in-class

c++ - C++ 编译器如何处理这个初始化列表?

当使用这样的初始化列表时:for(inti:{3,1,6,4}){std::cout输出顺序相同,3、1、6,最后是4。所以我知道编译器必须使用类似于std::vector的东西。而不是std::set.这是有保证的吗?我在哪里可以找到解释编译器必须如何解释{3,1,6,4}的文档. 最佳答案 您正在创建std::initializer_list的实例.参见http://en.cppreference.com/w/cpp/utility/initializer_list了解详情。从那个页面:Anobjectoftypestd::in

c++ - 作为数组的函数参数传递的初始化列表

我如何使这项工作:voidfoo(uint8_ta[]){...}foo({0x01,0x02,0x03});它给我一个错误:error:cannotconvert''to'uint8_t*{akaunsignedchar*}'forargument'1'^ 最佳答案 这对我有用,我不得不更改函数签名,但在我的情况下它实际上更好,因为它静态检查数组长度:voidfoo(std::arraya){/*usea.data()insteadofa*/}foo({0x01,0x02,0x03});//OKfoo({0x01,0x02});/

c++ - 将 int 转换为 size_t

我想知道当我传递integer时clang编译器的以下警告到std::initializer_list:non-constant-expressioncannotbenarrowedfromtype'int'to'unsignedlong'ininitializerlist为什么可以int被转换为size_t但是一个int不会传递给std::initializer_list,即intmain(){size_ts_t=0;inti=0;std::initializer_listi_l={i};//warnings_t=i;//nowarningreturn0;}

C++ std::list 排序保留顺序

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Isstd::list::sortstable?C++std::list排序函数是否保证保留列表中相等元素的顺序?例如。如果我们在列表中有对象A、B和C,并且比较运算符被重载,因此A==C和B

C++ : List iterator not incrementable

尝试删除列表的最后一个元素时出现此错误。我调试了代码并且能够找出导致它的原因和位置,这是我的代码:for(Drop_List_t::iteratori=Drop_System.begin();i!=Drop_System.end()&&!Drop_System_Disable;/**/){if(Player->BoundingBox.Intersect(&(*i)->BoundingBox)){i=Drop_System.erase(i);}++i;//Listiteratorcrasheshereiflastentrywasdeleted}我不知道我做错了什么……有什么建议吗?

java - 继承 Java 集合接口(interface)(Set、Map、List 等)的 C++ 等价物是什么?或者扩展 AbstractCollection?

我已经开始使用C++编写代码,来自Java背景(实际上我在我的大学学习了C++,但我们从未接触过STL等)无论如何,我已经到了在各种集合中排列数据的地步,我立即告诉自己“好吧,这是一种集合;这是一个列表,或者一个ArrayList;这是一张map等等。”在Java中,我会简单地让我正在编写的任何类实现Set或Map或List接口(interface);但我可能不会去继承ArrayList或HashSet或其他什么,那里的实现有点涉及,我不想把它们搞砸。现在,我要用C++(使用标准库)做什么?似乎没有Sets、Maps、Lists等的抽象基类——相当于Java接口(interface);