草庐IT

TYPE_STEP_COUNTER

全部标签

c++ - 是否有一种标准方法可以将容器<Type1> 转换为容器<Type2>?

我有两个类(class)A和B,并且存在从一个到另一个的隐式转换运算符,因此:Aa;Bb;b=a;//Works是否有标准的方法来转换std::list到std::list?(或者甚至从std::vector到std::list)。我知道我可以遍历列表并逐项构建第二个列表,但我想知道是否有更优雅的解决方案。不幸的是我不能使用boost但出于好奇,作为一个额外的问题,如果boost可以处理这个问题,我也很乐意知道如何处理。 最佳答案 嗯,是的。每个序列容器类型都有一个模板构造函数,它将一对迭代器(一个迭代器范围)作为输入。它可用于从一

c++ - 为什么 myClassObj++++ 不会产生编译错误 : '++' needs l-value just as buildin type do?

为什么myint++++使用VS2008编译器和gcc3.42编译器编译得很好??我期待编译器说需要左值,示例见下文。structMyInt{MyInt(inti):m_i(i){}MyInt&operator++()//returnreference,returnalvalue{m_i+=1;return*this;}//operator++needit'soperandtobeamodifiablelvalueMyIntoperator++(int)//returnacopy,returnarvalue{MyInttem(*this);++(*this);returntem;}in

c++ - "Unresolved overloaded function type"尝试将 for_each 与 C++ 中的迭代器和函数一起使用时

//for(unsignedinti=0;i我正在尝试使用for_each循环代替for循环进行赋值。我不确定为什么会收到此错误消息:Infunctionâvoidclean_entry(conststd::string&,std::string&)â:prog4.cc:62:40:error:nomatchingfunctionforcalltoâfor_each(std::basic_string::iterator,std::basic_string::iterator,)â 最佳答案 写:for_each(c.begin()

c++ - 如何修复错误 : unknown type name ‘namespace’

#ifndefUNO_ACTION_#defineUNO_ACTION_namespaceUno{namespaceGame{classGame;}}//namespacenamespaceUno{namespaceAction{using::Uno::Game::Game;classAction{public:virtualboolisDisposeable()=0;virtualvoidtakeAction(Game*game)=0;virtual~Action(){}};}}#endif我在ubuntu12.04上编译这些代码,它返回错误集:action.h:4:1:error:

c++ - 错误 : invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

为什么我会收到错误:从类型为“std::vector::reference{akastd::_Bit_reference}”的右值对类型为“bool&”的非常量引用进行无效初始化?vector>vis;bool&visited(intx,inty){returnvis[x][y];//error}据我所知,vector中的operator[]返回引用,所以它应该是一个左值,但它不起作用。我应该怎么做才能让它发挥作用? 最佳答案 那是因为std::vector不是它看起来的样子。std::vector有一个特化与类型bool-它是空间

c++ - 我什么时候应该使用 vector<int>::size_type 而不是 size_t?

在thisquestion我看到以下内容:for(vector::size_typeix=0;ix!=ivec.size();++ix){ivec[ix]=0;}我明白为什么int这里没有使用,但为什么不直接使用size_t?什么情况下应该用vector::size_type而不是size_t? 最佳答案 初级使用时间size_type在模板中。虽然std::vector::size_type通常是size_t,some_other_container::size_type可能是其他类型1。允许用户添加到std的少数内容之一命名空间

Kotlin,针对 Java 互操作 : Idiomatic type for lazy collection?

当面向Java互操作时,惰性集合应该使用什么类型?Sequence对Kotlin调用者最有意义,因为它的扩展函数默认是惰性的,但强制Java调用者处理Kotlin标准库类型并手动转换序列迭代器(序列不扩展可迭代!)Iterable由于for循环中的隐式使用,对Java调用者来说是有意义的,但会导致毫无戒心的Kotlin调用者由于非惰性扩展函数而意外放弃惰性Stream最适合Java和Kotlin调用者,但可能有开销,并且是Java8+(Kotlin目标6+) 最佳答案 您可以通过实现所有这三个来让每个人都开心。例如:dataclas

Kotlin,针对 Java 互操作 : Idiomatic type for lazy collection?

当面向Java互操作时,惰性集合应该使用什么类型?Sequence对Kotlin调用者最有意义,因为它的扩展函数默认是惰性的,但强制Java调用者处理Kotlin标准库类型并手动转换序列迭代器(序列不扩展可迭代!)Iterable由于for循环中的隐式使用,对Java调用者来说是有意义的,但会导致毫无戒心的Kotlin调用者由于非惰性扩展函数而意外放弃惰性Stream最适合Java和Kotlin调用者,但可能有开销,并且是Java8+(Kotlin目标6+) 最佳答案 您可以通过实现所有这三个来让每个人都开心。例如:dataclas

c++ - 为什么 std::result_of<int(int)>::type 无效?

我已阅读以下相关问题:std::result_ofsimplefunctiondecltype,result_of,ortypeof?和thepageonstd::result_ofatcppreference.com.所有这些似乎都表明我应该能够使用:std::result_of::typev1=10;但是,当我尝试使用g++4.9.2构建以下程序时#includeintfoo(){return0;}intmain(){std::result_of::typev1=10;//LINEAstd::result_of::typev2=20;//LINEBreturn0;}我收到“LINE

c++ - C++11 <type_traits> 模板参数类型推导失败

我正在尝试了解如何使用C++(11).这是我的简单测试程序#includetemplateinlineUadd(typenamestd::enable_if::value,U>::typea,typenamestd::enable_if::value,S>::typeb){returna+b;}intmain(intargc,constchar*argv[],constchar*envp[]){unsignedintui;inti;autoa=add(ui,i);return0;}当使用GCC4.8.1编译时,它会出错/home/per/f.cpp:Infunction‘intmain