草庐IT

back_emplace_iterator

全部标签

c++11:使用 const_iterator 删除

我相信从C++11开始,erase大多数容器的功能(例如std::vector)接受const_iterator作为参数:iteratorerase(const_iteratorposition);我的编译器(GCC4.8和Clang3.2,都使用GCClibstdc++)仍然不允许我使用这样的函数,即使在使用--std=c++11编译时也是如此。.是编译器/libstdc++错误,还是我做错了什么?这是示例代码:#includeintmain(){std::vectorv;v.push_back(1);v.push_back(2);v.push_back(3);std::vector

c++ - std::make_unique(以及 emplace、emplace_back)对 initializer_list 参数的笨拙推导

假设我有这个结构:structposition{intx,y;};和另一个将this作为构造函数参数的类:classpositioned{public:positioned(positionp):pos(p){}private:positionpos;};我怎样才能得到简单的autobla=std::make_unique({1,2});上类?目前,编译器试图通过initializer_list匹配并调用make_unique的数组变体,这很愚蠢,因为positioned只有一个构造函数。emplace出现同样的问题和emplace_back功能。几乎所有将其可变模板参数转发给类的构造

c++ - 为什么 emplace_back ("Hello") 调用 strlen?

Justin'sanswer在另一个问题上做了一个我觉得很有趣但无法解释的观察。考虑以下代码:std::vectorv;v.push_back("Hello,world!");//Doesn'tcallstrlen.v.emplace_back("Hello,world!");//Callsstrlen.如果您查看程序集,emplace_backgeneratesacalltostrlen,而push_backdoesnot(使用-Ofast在gcc8.1和clang6.0中测试)。为什么会这样?为什么不能emplace_back优化strlen在这里打电话?我最初的想法是push_b

c++ - 如何将 BOOST_FOREACH 与仅支持 const_iterator 的容器一起使用?

我有这个容器:class/*final*/Row{public:typedefFieldIteratorconst_iterator;typedefFieldIteratoriterator;FieldIteratorbegin()const;FieldIteratorend()const;FieldIteratorbegin();FieldIteratorend();...};鉴于此,以下代码可以正常编译:BOOST_FOREACH(Fieldfield,row){}但是,Row类不应该有可变迭代器,所以我更改了Row类,删除了可变访问:class/*final*/Row{publi

c++ - 重载 >> istream_iterator 对

这个问题在这里已经有了答案:Dependentnameresolution&namespacestd/StandardLibrary(1个回答)关闭7年前。我正在尝试构建对上的ifstream_iterator。我的代码如下:typedefpairT;istream&operator>>(istream&stream,T&in){stream>>in.first>>in.second;returnstream;}intmain(intargc,char**argv){ifstreaminfile("dummy2");istream_iteratoriit(infile);istream

c++ - Visual Studio regex_iterator 错误?

我在使用VisualStudio2013,我看到了一个我认为是错误的东西,我希望有人可以确认吗?stringfoo{"A\nB\rC\n\r"};vectorbar;for(sregex_iteratori(foo.cbegin(),foo.cend(),regex("(.*)[\n\r]{1,2}"));i!=sregex_iterator();++i){bar.push_back(i->operator[](1).str());}此代码在VisualStudio正则表达式库中命中调试断言:regex_iteratororphaned如果我在for循环之外定义regex没问题:str

c++ - 用 gcc 编译 std::regex_iterator

我可以使用g++-ctest.cpp-std=c++0x创建.o文件,但无法链接它,出现下一个错误:test.cpp:(.text+0xe5):undefinedreferenceto`std::regex_iterator>::regex_iterator(charconst*,charconst*,std::basic_regex>const&,std::bitset)'test.cpp:(.text+0xf1):undefinedreferenceto`std::regex_iterator>::regex_iterator()'代码:#include#include#inclu

c++ - std::vector::push_back 不可复制对象给出编译器错误

我在g++(GCC)4.7.2上遇到编译错误但不在MSVC-2012当试图std::vector::push_back不可复制(私有(private)复制构造函数)但可移动的对象。对我来说,我的示例看起来与SO和其他地方的许多其他示例相同。错误消息使它看起来像是结构不是“可直接构造”的问题-我不知道这意味着什么,所以我加倍不确定为什么对象需要“可直接构造”才能被推回。#include#includestructMyStruct{MyStruct(std::unique_ptrp);MyStruct(MyStruct&&other);MyStruct&operator=(MyStruct

c++ - 为什么 push_back 有两个左值和右值重载?

为什么一个类/函数会有两个重载,一个用于左值,一个用于右值?例如,来自this视频,它说我们有两个重载vector::push_backvoidpush_back(constT&value);voidpush_back(T&&value);为什么我们不能只有一个值重载,voidpush_back(Tvalue);如果它是一个左值,值将被复制,如果它是一个右值,值将被move。这不是标准的工作方式和保证方式吗? 最佳答案 根据您的按值提议,技术上会有复制+move或move+move,而对于其他两个重载,只有一个拷贝或一个move。

c++ - 元组到元组的 std::map 和使用 emplace

考虑以下代码,使用g++7.0.1(-std=c++17)编译:#include#includeintmain(){//CreateanaliasforatupleofthreeintsusingThreeTuple=std::tuple;//Createanaliasforamapoftupletotuple(ofthreeints)usingMapThreeTupleToThreeTuple=std::map;MapThreeTupleToThreeTuplem;//ThefollowingdoesNOTcompilem.emplace({1,2,3},{4,5,6});//...