草庐IT

pairing_iterator

全部标签

c++ - 按字符串对 std::vector<std::pair<std::string,bool>> 进行排序?

我如何通过比较pair.first这是一个std::string来对这个vector进行排序?(不提供静态比较功能,也不使用boost)。 最佳答案 std::vector>v;std::sort(v.begin(),v.end());std::pair过载operator首先按first排序元素然后由second元素。因此,如果您只是对vector进行排序使用默认排序顺序(operator),您将获得所需的顺序。 关于c++-按字符串对std::vector>进行排序?,我们在Stac

c++ - gtk_widget_add_tick_callback() 和 gtk_main_iteration()

我有两个GTK窗口运行动画的普通(主)窗口,在gtk_widget_add_tick_callback()注册的回调中绘制内容。在某个时候会创建运行模态循环的辅助窗口:voidshow_modal(){GtkWindow*gw=gtkwindow(this);if(parent())gtk_window_set_transient_for(gw,gtkwindow(parent()));gtk_widget_show(GTK_WIDGET(gw));gtk_window_set_modal(gw,TRUE);gtk_window_set_keep_above(gw,TRUE);this

c++ - std::map<K,V>::iterator 是否实例化 std::map<K,V>?

我有这段代码,它适用于GCC:#includeclassFoo;classBar;typedefstd::mapMyMap;MyMap::iteratori;classFoo{MyMap::iteratorsome_data;};当前设计的代码(这是令人不快的循环,是的,我坚持使用它)需要map::iterator可用于Foo和Bar.之所以有效,是因为GCC库实现恰好不需要实例化映射的键类型来实例化迭代器。这是有保证的吗?在定义映射迭代器类型时,该标准似乎有些放任自流。这段代码的可移植性如何? 最佳答案 这会导致未定义的行为。在声

c++ - 'iterator' 可以只输入子类 'const_iterator' 吗?

在anotherquestionaboutiterators之后我对自定义容器有一些疑问。在我的容器中,iterator是const_iterator的子类,因此我可以“免费”从非const转换为const。但这是否允许,或者这样的设置是否有任何缺点或不工作的情况? 最佳答案 是的,这很好。这就是VC10对vector的迭代器的实现方式。例如,是结构化的。见_Vector_iterator和_Vector_const_iterator在.顺便说一句,编写迭代器很困难。值得您花时间学习和使用boost::iterator库。

c++ - 将 std::pair<T1, T2> const 转换为 std::pair<T1 const, T2> const 安全吗?

reinterpret_cast是否安全(理论上或实际上)一个std::pairconst&变成std::pairconst&,假设程序员没有故意做一些奇怪的事情,比如专门std::pair? 最佳答案 这样做并不便携。std::pair要求在第20.3节中列出。第17.5.2.3条阐明了Clauses18through30andAnnexDdonotspecifytherepresentationofclasses,andintentionallyomitspecificationofclassmembers.Animplemen

c++ - 我应该在哪里为我的 std::pair 特化定义运算符 >>?

考虑以下程序:#include#include#include#includeusingnamespacestd;//justforconvenience,illustrationonlytypedefpairpoint;//thisismyspecializationofpair.Icallitpointistream&operator>>(istream&in,point&p){returnin>>p.first>>p.second;}intmain(){vectorv((istream_iterator(cin)),istream_iterator());//^^^^^^//ex

c++ - 以 std::pair 作为键创建 std::unordered_map

我正在尝试创建一个以std::pair作为键的std::unordered_map。你可以想象,这需要我显式地提供一个类来为给定的键生成哈希,以及键的相等比较器。到目前为止,这是我的代码:#include#include#includetemplatestructPairHash{size_toperator()(conststd::pair&key){returnstd::hash()(key.first)^std::hash()(key.second);}};templatestructPairEqual{booloperator()(conststd::pair&lhs,cons

c++ - 我可以使用 std::pair,但重命名 .first 和 .second 成员名称吗?

我遇到的一个常见设计问题是,我将两个变量捆绑在一起,然后失去以有意义的方式引用它们的能力。std::paircords;cord.first=0;//is.firstthexorycoordinate?cord.second=0;//is.secondthexorycoordinate?我考虑过编写基本结构,但是我失去了很多std::pair:带来的好处make_pair非成员重载运算符交换得到等等有没有办法为first和second数据成员重命名或提供替代标识符?我希望利用所有接受std::pair的函数,但仍然可以通过以下方式使用它们:std::paircords;//specia

c++ - 为什么 vector::iterator 在重新分配时无效?

我不明白为什么在重新分配发生时vector的迭代器应该失效。难道不能简单地通过在迭代器中存储一个偏移量——而不是一个指针——来防止这种情况吗?为什么vector不是这样设计的? 最佳答案 只是为与性能相关的理由添加一个引用:在设计C++时,Stroustrup认为模板类如std::vector是至关重要的。接近原生数组的性能特点:Onereasonfortheemphasisonrun-timeefficiency...wasthatIwantedtemplatestobeefficientenoughintimeandspacet

c++ - 将 std::list<>::iterator 的值指向指针?

如何循环通过STL::List并存储其中一个对象的值以供稍后在函数中使用?Particle*closestParticle;for(list::iteratorp1=mParticles.begin();p1!=mParticles.end();++p1){//ExtrastuffremovedclosestParticle=p1;//failstocompile(editfromcomments)} 最佳答案 要么Particle*closestParticle;for(list::iteratorit=mParticles.be