草庐IT

shared_int

全部标签

c++ - vector<shared_ptr<T>> 和 vector<shared_ptr<const T>> 之间的高效转换

我有一个类:classX{vector>v_;public:vector>getTs(){returnv_;}};它有一个vector的shared_ptr类型T.出于某种原因,它需要公开一个方法来返回这个vector。但是,我不想修改vector的内容,也不想指向对象。所以我需要返回一个vector的shared_ptr.我的问题是,有什么有效的方法可以做到这一点吗?如果我简单地返回它,它可以工作,但它需要重建一个vector,这有点昂贵。谢谢。 最佳答案 你不能直接这样做-但你可以在你的容器上定义“View”,让你做一些非常相似

c++ - 如何在 C++ 中获取具有给定位模式(如 int32_t)的 float ?

我需要一种快速获取float的方法具有给定的位模式(提供为int32_t)。当然,编译器应该优化整个结构。简单转换执行强制转换和reinterpret_cast不允许... 最佳答案 编译器会优化它是不可靠的,但它避免了UB,前提是提供的值确实是float的表示(也就是说,它的大小正确并且它的位模式不包含陷阱表示float)。GCC至少有时能够优化它:floatconvert(int32_tinputvalue){floatf;std::memcpy(&f,&inputvalue,sizeof(f));returnf;}如果优化是问

c++ - vector<pair<int,int>> 上界

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭9年前。我正在尝试使用upper_bound在vector>上,像这样:vector>data;autoup=upper_bound(data.begin(),data.end(),0);VS2012给我以下错误:errorC2784:'boolstd::operator&,conststd::vector&)':couldnotdeducetemplatearg

c++ - `intmax_t` 在具有 64 位 `long int` 和 `long long int` 的平台上应该是什么?

在C++标准18.4中它指定:typedef'signedintegertype'intmax_t;根据具有64位longint和64位longlongint的平台标准,这个“有符号整数类型”应该是?请注意,longint和longlongint是不同的基本类型。C++标准说:Theheaderdefinesallfunctions,types,andmacrosthesameas7.18intheCstandard.在C标准(N1548)的7.18中,它说:Thefollowingtypedesignatesasignedintegertypecapableofrepresentin

c++ - 反转 map<string, int> 到 vector<vector<string>> 的映射

以我的priorquestion为基础,我有一个单词的映射及其计数存储在map中.我想反转它,以便将所有具有相同计数的单词组合在一起。我的解决方案是使用vector>.第一个vector的索引是计数,第二个vector是单词的集合。阅读上一个问题的答案后,这里是我一直在努力工作的内容:vector>sorted_words;for(map::const_iteratorit=counters.begin();it!=counters.end();++it){coutfirstsecondit->second&&sorted_words[it->second].size()>0){cou

c++ - 如何在现代 C++ 中将 float 转换为 int

尽管看起来很奇怪,但我找不到如何将float干净地转换为int。这个技巧intint_value=(int)(float_value+0.5);触发一个warning:useofold-stylecast在海合会中。那么,将float转换为int的现代风格的简单方法是什么?(我当然接受精度的损失) 最佳答案 正如Josh在评论中指出的那样,+0.5不是很可靠。为了额外的安全,您可以像这样将static_cast与std::round结合起来:intint_value=static_cast(std::round(float_valu

c++ - 将 unsigned int 写入二进制文件

我第一次使用二进制文件,我的手上有一撮头发。无论如何,我有以下定义:unsignedintcols,rows;这些变量可以是1到大约500之间的任何值。当我将它们写入二进制文件时,我正在这样做:myFile.write(reinterpret_cast(&cols),sizeof(cols));myFile.write(reinterpret_cast(&rows),sizeof(rows));当我返回读取文件时,在cols=300上,我得到的结果是:44100有人可以向我解释为什么我会得到这个结果吗?我不能说有什么不对,因为我真的认为是我不懂事。我想做的是按原样将值存储在文件中,这样

c++ - 在顶层使用 shared_ptr 而不是 scoped_ptr 有什么优势吗?

我的团队对于指针容器在特定上下文中的使用存在一些分歧。请考虑:intmain(){//Toplevel.Thisisanimportantfacttothecontext//i.e.thatthefollowinginstanceisatthislevel//sothatitsmembersareessentiallyatprogramscope.MainClassmainClass;mainClass.run();}//AinstanceofaclassderivedfromBufferdoessomethingverycomplex//(ithasvarioushandlestor

c++ - 基于模板参数是否为 shared_ptr 的函数特化

我正在尝试检测类型是否为shared_ptr如果是,则分派(dispatch)到特定函数模板或覆盖。这是我实际尝试的简化版本:#include#include#includetemplatestructis_shared_ptr:std::false_type{};templatestructis_shared_ptr>:std::true_type{};classFoo{};typedefstd::shared_ptrSharedFoo;templatevoidgetValue();template::value>::type=0>voidgetValue(){printf("sha

c++ - "no ' operator++(int) ' declared for postfix '++ ' [-fpermissive]"枚举

这个问题在这里已经有了答案:HowcanIiterateoveranenum?(28个答案)Whycan'tIincrementavariableofanenumeratedtype?(10个答案)关闭9年前。我有枚举enumProgramID{A=0,B=1,C=2,MIN_PROGRAM_ID=A,MAX_PROGRAM_ID=C,}CurrentProgram;现在,我正尝试像这样递增CurrentProgram:CurrentProgram++,但编译器提示:没有为后缀'+声明'operator++(int)'+'[-fpermissive]。我认为有这样一个运算符可以增加“枚