草庐IT

c++ - 如何访问 std::vector 类型的成员

我的程序有一个计算总和的函数,为此我需要访问vector中对象的属性:vector声明:classtrilateration{public:...std::vector*potential;...};然后在构造函数中对其进行初始化:trilateration::trilateration(){...potential=newstd::vector();...}类(class)提示如下所示:classtip{public:doublesum;Point2d*pt;tip();tip(doublex,doubley);virtual~tip();};提示构造函数:tip::tip(doub

c++ - std::allocator_traits 默认为具有多个模板参数的分配器

std::allocator_traits当我提供一个带有单个模板参数的分配器的STL样式容器时,它会自动发挥它的魔力,但当我提供一个带有两个模板参数但其他方面相似的分配器的STL样式容器时,它不会自动发挥作用。我需要做什么来告诉std::allocator_traits如何与具有多个模板参数的分配器交互?是否有可能获得std::allocator_traits在这种情况下提供合理的默认值?例如,如果我采用HowardHinnant在AllocatorBoilerplate中提供的简单分配器并将其提供给std::vector那么一切都很好。如果我添加一个虚拟intallocator的参

c++ - 通过来自任意索引的参数包初始化 std::array

通过可变模板参数初始化一个std::array,从给定的索引开始可以通过以下方式完成:#includetemplatestructA{templateA(size_ti,Ts...vals){constexprsize_tP=sizeof...(vals);std::arraytemp{vals...};for(size_tj=0;jarr;};但是否可以在不将参数包转换为临时元组或另一个std::array的情况下实现相同的目的? 最佳答案 您可以使用std::index_sequence和委托(delegate)构造函数:tem

c++ - 在 std::bind 中使用 std::bind :编译错误(隐式转换)

这个问题在这里已经有了答案:std::bindaboundfunction(2个答案)关闭6年前。当我想创建一个std::function来包装work(..)成员时,我遇到了一个让我很累的编译错误。示例代码:classC{public:C(){std::functionf=std::bind(&C::work,this,std::bind(&C::step1,this),std::bind(&C::step2,this));QListlst;lst.append(f);.....}private:voidwork(std::functionfn1,std::functionfn2){

c++ - 为什么这个自定义比较器在构造 std::priority_queue 时失败,而它适用于 std::sort?

比较器comp定义如下。它适用于std::sort,但无法在std::priority_queue的构造函数中编译。问题是什么?谢谢。#include#include#includeusingnamespacestd;boolcomp(inta,intb){returna>b;}intmain(){vectorvec={4,2,1,3};sort(vec.begin(),vec.end(),comp);//OKpriority_queueq1(less(),vec);//OKpriority_queueq2(comp,vec);//Failreturn0;}错误信息:error:nom

c++ - std::experimental::optional inside constexpr 函数

我想在我的constexpr函数中使用可选的习惯用法来轻松地阐明变量是否已设置。我对std::experimental::optional的尝试:constexprboolcall(){std::experimental::optionalr;r=true;//Error//Similarerrorwith://r=std::experimental::optional(true);if(!r){returnfalse;}return*r;}我得到错误:调用非constexpr函数-所以赋值是不可能的,因为这个操作不能是constexpr(Example)。但如果我实现自己的(非常丑陋

c++ - std::getline 正在读取指定分隔符不存在的行?

我想从以下字符串(存储在文件中)的数组中打印控制台中的每个对象:{beforechars[{Object1},{Object2},{Object3}]afterchars}我是这样做的:std::ifstreamis("content.txt");std::getline(is,content,'[');while(std::getline(is,content,'{')){std::getline(is,content,'}');std::cout但是我得到了这个输出:Object1Object2Object3]afterchars}我的理解是,在Object3迭代之后,ifstre

c++ - 构造一个 constexpr std::weak_ptr

根据std::weak_ptr文档可以构造一个constexprweak_ptr:#includeconstexprweak_ptrfoo{};但是,使用clang尝试此操作会产生编译错误,提示constexpr变量不能具有非文字类型'conststd::weak_ptr',这是因为weak_ptr有一个用户提供的析构函数。(确实如此,查看libc++header)我的问题是,这是一个libc++错误,还是做constexprweak_ptr只是没有意义并且拥有constexpr默认构造函数是一个错误?我可以期待它在未来发挥作用吗? 最佳答案

c++ - 使用与 double 数组的内存匹配的 std::vector

我正在尝试连接两个现有代码库——一个使用C,另一个使用C++。C++代码使用std::vector而另一个基于double组。我想从C代码传递double组,在C++代码中对std::vectors执行操作,并最终将这些操作反射(reflect)在double组中。是否可以创建一个std::vector来匹配double数组占用的内存?我尝试了几种选择,但它们都涉及创建一个新vector和将double数组的拷贝复制到该vector中。例如:voidfcn(double*a,intsizeofa){std::vectorvect_a;vect_a.assign(a,a+sizeofa)

c++ - std::transform with lambda: 跳过一些项目

我有一些C++11代码,比如std::vectornames;std::mapfirst_to_last_name_map;std::transform(names.begin(),names.end(),std::inserter(first_to_last_name_map,first_to_last_name_map.begin()),[](conststd::string&i){if(i=="bad")returnstd::pair("bad","bad");//Don'tWantThiselsereturnstd::pair(i.substr(0,5),i.substr(5,