草庐IT

auto-increment-increment

全部标签

c++ - foreach(int i.. 和 foreach(auto i

我正在MacOX(LLVM4.2)附带的Clang编译器上试验C++11功能,以下结果让我感到困惑://clangcompilewith"c++-std=c++11-stdlib=libc++"#include#includeintmain(void){usingnamespacestd;vectoralist={1,2,3,4};for(inti=0;i在运行环境中,我得到如下不同的输出:12342340为什么我会得到不同的结果? 最佳答案 for(autoi:alist)这会获取alist中的每个value,因此i变为:1,2,

C++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 'for(auto &str : vec)' 内部 for 循环的目的是什么?

我是C++的新手,正在尝试学习vector的概念。我在网上看到这段代码。我的问题是,'for(auto&str:vec)'中的内部for循环的目的是什么?为什么作者要对第一个引用(&str)创建第二个引用(&c)?intmain(){vectorvec;for(stringword;cin>>word;vec.push_back(word)){}for(auto&str:vec){for(auto&c:str){c=toupper(c);}}for(inti=0;i!=vec.size();++i){if(i!=0&&i%8==0)cout 最佳答案

c++ - 带有 if 语句的 auto 函数不会返回值

我制作了一个模板和一个auto函数,用于比较2个值并返回最小值。这是我的代码:#includeusingnamespacestd;//Templatewithavaluereturningfunction:PrintSmallertemplateautoPrintSmaller(TNumOne,UNumTwo){if(NumOne>NumTwo){returnNumTwo;}else{returnNumOne;}}intmain(){intiA=345;floatfB=23.4243;cout但它无法编译,我在VS2015上遇到此错误:错误C3487“int”:所有返回表达式必须推导出

c++ - 我可以使用 auto 或 decltype 代替尾随返回类型吗?

我发现尾随返回类型很容易定义返回复杂类型的函数的返回值,例如:autoget_diag(int(&ar)[3][3])->int(&)[3]{//usingtrailingreturntypestaticintdiag[3]{ar[0][0],ar[1][1],ar[2][2]};returndiag;}auto&get_diag2(int(&ar)[3][3]){//adding&autobecauseotherwiseitconvertsthearraytopointerstaticintdiag[3]{ar[0][0],ar[1][1],ar[2][2]};returndiag;

C++——这里是否存在从 Fred* 到 auto_ptr<Fred> 的隐式转换?

我看到了下面的代码,#include#includeusingnamespacestd;classFred;//Forwarddeclarationtypedefauto_ptrFredPtr;classFred{public:staticFredPtrcreate(inti){returnnewFred(i);//Isthereanimplicitcastinghere?Ifnot,howcanwereturn//aFred*withreturnvalueasFredPtr?}private:Fred(inti=10):i_(i){}Fred(constFred&x):i_(x.i_

c++ - 我应该明确地零初始化 auto_ptr 吗?

我的一些同事更喜欢在构造函数初始化列表中将std::auto_ptr显式初始化为0,但它会被初始化为0在它的构造函数中没有任何显式初始化。那么有什么理由这样做吗?#includeclassA{A():SomePtr(0){}private:std::auto_ptrSomePtr;}; 最佳答案 不,std::auto_ptr的默认构造函数正是这样做的,因此没有必要显式地这样做。无论如何,这是风格问题,您应该保持一致。例如,您是否会在构造函数初始化列表中显式调用成员vector的默认构造函数?作为旁注,std::auto_ptr在即

c++ - 我可以创建一个 auto_ptr 数组吗?

我有一个基类,它被多个派生类继承。我想创建baseClass指针的自动指针数组。当我初始化那些自动指针时,我遇到了一些编译时错误,然后我尝试这样做std::auto_ptrpbase[3];std::auto_ptrb1(newderived1());std::auto_ptrb2(newderived2());std::suto_ptrb3(newderived3());pbase[0]=b1;pbase[1]=b2;pbase[2]=b3;它工作正常,我修复了内存泄漏问题,而我是一个窗口,我不使用valgrind,我使用boost框架来解决泄漏问题。对于编译错误:classA{pu

c++ - 我们是否应该始终对局部变量使用 auto&&

阅读此主题后auto&&,这是否意味着我们在声明局部变量以捕获函数的返回类型时应该始终使用auto&&而不是auto(以准确保留功能)?用例可以是例如auto&&result=func_returning_lvalue_or_lvalue_reference();或auto&&iterator=vector_.begin();或其他任何内容。换句话说,有很多auto&&的基本代码是正常的吗? 最佳答案 不。您不应该一直使用auto&&。特别是,如果您需要拷贝,则不应使用它。使用auto&&,您可能会获得一个拷贝(作为对临时对象的引用

c++ - 这是一个很好的 std::auto_ptr<> 用例吗?

请假设我有一个接受指针作为参数的函数。这个函数可以抛出异常,因为它使用std::vector::push_back()管理此指针的生命周期。如果我这样声明:voidmanage(T*ptr);并这样称呼它:manage(newT());如果它抛出异常将指针插入std::vector,我实际上有内存泄漏,不是吗?会像这样声明函数:voidmanage(std::auto_ptrptr);解决我的问题?我希望它首先分配std::auto_ptr在堆栈上(我猜永远不会抛出异常的东西)并让它获得对指针的所有权。安全的。然后,在函数内部,我将原始指针插入std::vector,这也是安全的:如果