草庐IT

const_iterators

全部标签

c++ - 在方法/函数中使用 `static const std::string` 还是只使用 `const std::string` 更好?

我有一个方法/函数:voidfoo(){staticconststd::stringstrSQLQuery="SELECT...";//ormaybeconststd::stringstrSQLQuery="SELECT...";//someoperationsonstrSQLQuery//i.e.concatenatingwithWHERE,etc.:conststd::stringstrSQL=strSQLQuery+strWHERE;doSthOnDataBase(strSQL);}(SQL只是一个例子)staticconst只会被初始化一次,但会一直保存在内存中直到进程结束。c

c++ - 为什么未初始化的 const 成员在 C 中的处理方式与在 C++ 中的处理方式不同?

这个问题在这里已经有了答案:constinCvsconstinC++(5个答案)关闭7年前。我为要链接的库包含一个Cheader。header有一个结构foo,我想在其中声明一个新变量bar。我收到编译器错误:error:uninitializedconstmemberin"structfoo"这些成员需要初始化是有道理的,因为以后不能为它们赋值。但是使用这个库的C程序可以做完全相同的事情并且它可以工作。C和C++标准有区别吗?这只是一个示例。实际上,我指的是libmtd.h(mtd-utils)中的结构mtd_dev_info。http://mtd-utils.sourcearchi

c++ 为什么 const& 可以得到 const 值?

这个问题在这里已经有了答案:WhathappenswhenIassignatemporaryinttoaconstreferenceinC++?[duplicate](2个答案)关闭8年前。我明白为什么int&x=1是非法的(你不能引用常量值),但我不明白为什么constint&x=1合法吗?你怎么能引用数字“1”?它甚至不是定义的变量。编辑:我阅读了这篇文章中给出的答案:WhathappenswhenIassignatemporaryinttoaconstreferenceinC++?但有人可以解释他的意思是“表达式f(1)返回的临时值的生命周期将延长其生命周期。此规则对于const

c++ - 对 'cv::viz::Viz3d::Viz3d(std::string&const)' 的 undefined reference

我已经使用qtcreator运行了我的opencv代码,当我尝试使用Viz库时得到了这个答案。代码:#include#include#include#include#include///Createawindowviz::Viz3dmyWindow("VizDemo");///StarteventloopmyWindow.spin();///Eventloopisoverwhenpressedq,Q,e,Eprintf("Firsteventloopisover\n");///Accesswindowviaitsnameviz::Viz3dsameWindow=viz::getWind

c++ - 为什么箭头运算符 "->"不能在 boost::numeric::ublas::vector<...>::iterator 上工作?

考虑这段代码:structCData{intbar(){return1;}};intmain(){typedefboost::numeric::ublas::vectorvec_data_t;vec_data_tfoo;for(vec_data_t::iteratorit=foo.begin();it!=foo.end();++it){std::coutbar()为什么循环中使用箭头运算符的第一行编译失败,而使用运算符*的下一行编译正常?我习惯于将箭头运算符与std容器迭代器一起使用,想知道为什么它在boost::numeric::ublas迭代器上失败。我使用的是boost1.54和

c++ - 在 C++ 中将惰性生成器实现为 forward_iterator

MyGenerator代表一个(可能)有限的整数序列,计算起来很昂贵。所以我不想预先生成它们并将它们放入容器中。structMyGenerator{boolHasNext();intNext();}全部打印:MyGeneratorgenerator;while(generator.HasNext()){std::cout如何实现类似的遵循forward_iterator协议(protocol)的生成器?boost::function_input_iterator接近,但我不知道预先元素的数量。 最佳答案 首先,查看boost::fu

c++ - 按值或 const 引用传递函数?

我应该按值还是按对一个函数的引用来传递std::string。此函数将此值存储在类的成员变量中。当谈到值传递或引用传递时,我总是感到困惑。请消除我对此的困惑。代码如下:classDataStore{public:voidaddFile(conststring&filename,constset&filePaths){if(dataStoreMap.insert(make_pair(filename,filePaths)).second){cout>dataStoreMap;};我应该像这样声明函数吗:voidaddFile(conststring&filename,constset&f

c++ - 为什么 const 类成员必须是静态的才能得到适当的优化?

给定:classFoo{constintx=5;public:inlineintget(){returnx;}};classBar{staticconstintx=5;public:inlineintget(){returnx;}};intfn0(Foo&f){returnf.get();}intfn1(Bar&b){returnb.get();}编译后的输出提供内存获取以读取fn0()中x的值,而添加static结果是文字5被内联到fn1()中。这意味着只有当整数常量为静态时,get()的调用方才可以像使用常量代替get()一样进行优化。我有更复杂的情况,其中static不合适。派生

c++ - 自动转换模板<T>到模板<const T>

下面的这段应该主要用于T={char,constchar}的字符串View是主要的预期模板实例化目标。cmp函数应该类似于strcmp来比较View。.问题是虽然char*愉快地转换为constchar*我不知道如何获得SVec转换为SVec一样开心。最后一行(cout)无法编译。我必须明确地进行转换(cmp(SVec(rv),rvc))。它可以像char*一样自动吗?至constchar*?代码(大大简化):templateclassSVec{protected:T*begin_;size_tsize_;public:SVec(T*begin,size_tsize):begin_(b

c++ - 为什么 static const char * template struct 成员没有初始化

我有一些C++11模板代码,我正在尝试移植到VisualC++Compiler2015。原始代码工作得很好,但是我需要重写它以解决constexpr的问题。Theoriginalcode(simplifiedexample)#includestructString{staticconstexprconstchar*value{"STRING"};};templateclassDerived{public:staticconstexprconstchar*value{Base::value};};templatestructFoo{staticconstexprconstchar*val