草庐IT

c++ - 结构的通用比较运算符

在我的许多单元测试中,我需要比较只有数据成员的简单结构的内容:structObject{intstart;intstop;std::stringmessage;}现在,如果我想写这样的东西:CHECK(object1==object2);我总是要实现:booloperator==(constObject&lhs,constObject&rhs){returnlhs.start==rhs.start&&lhs.stop==rhs.stop&&lhs.message=rhs.message;}编写所有这些比较函数变得乏味,但也容易出错。试想一下,如果我向Object添加一个新的数据成员,但

c++ - std::regex——是否有一些需要链接的库?

我收到以下代码的链接器错误:#includeintmain(){std::regexrgx("ello");return0;}test.o:Infunction`basic_regex':/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/tr1_impl/regex:769:undefinedreferenceto`std::basic_regex>::_M_compile()'collect2:ldreturned1exitstatus 最佳答案

c++ - constexpr std::array with static_assert

#include#includeintmain(intargc,char**argv){constexprconststd::arrayarr{{0,1}};constexprconstintarr2[]={0,1};static_assert(arr[0]==arr2[0],"asdf");static_assert(arr[1]==arr2[1],"asdfasdf");return0;}当使用gcc4.8.2和4.9.1使用g++test.cpp--std=c++11编译时,编译成功。但是,当使用clang++test.cpp--std=c++11使用clang3.4和3.5编译

c++ - 如何使用不同类型的键搜索 std::map

如果我有std::map,使用Y的实例在map中查找匹配项目的最佳方式是什么??假定Y中的信息足以唯一地找到X,但出于性能原因,我不想创建X的实例通过复制Y值(value)观。我意识到我可以通过为X创建一个公共(public)基类或接口(interface)来做到这一点和Y并将其设为map键,但还有其他方法吗?例如创建某种比较器对象?为清楚起见,这里是示例代码:classX{public:intid;intsubId;};std::mapdetailsMap;classY{public:intgetId();intgetSubId();intsomeOtherUnrelatedThin

c++ - 为什么在 std::vector 的初始化列表中调用复制构造函数?

我有以下非常简单的类:classFoo{public:Foo(){}Foo(constFoo&)=delete;Foo(Foo&&){}voidoperator=(constFoo&)=delete;voidoperator=(Foo&&){}voiddump()const{}};该类是可move构造和可赋值的,但不是可复制构造和可赋值的。我想使用vector的初始化列表来初始化Foo元素的vector。std::vectorvf={Foo()};编译器会报错,因为代码必须使用已删除的复制构造函数。谁能解释一下,为什么在这种情况下不使用move构造,为什么需要对象的拷贝?以下也需要复制

c++ - std::iostream 是非阻塞的吗?

根据Boost.Iostreams的提升引用(在第3.6节中,最底部):http://www.boost.org/doc/libs/1_64_0/libs/iostreams/doc/index.htmlAlthoughtheBoost.IostreamsFilterandDeviceconceptscanaccommodatenon-blockingi/o,theC++standardlibrarystreamandstreambufferinterfacescannot,sincetheylackameanstodistinguishbetweentemporaryandperma

c++ - 如何编写适用于映射和对 vector 的 C++ 模板>

我想编写一个模板函数来遍历std::pair的容器并返回一个包含这两种类型的模板值。我已经为std::map工作了如下:templatestd::pair,std::vector>unzip(conststd::map&zipped){autounzipped=std::make_pair(std::vector(),std::vector());for(auto&one_two:zipped){unzipped.first.push_back(one_two.first);unzipped.second.push_back(one_two.second);}returnunzippe

c++ - 在 map 中使用 unique_ptr 时删除 std::pair 中的函数

我有一段C++代码,我不确定它是否正确。考虑以下代码。#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){vector>>v;v.resize(5);returnEXIT_SUCCESS;}GCC编译这段代码没有问题。然而,英特尔编译器(版本19)因错误而停止:/usr/local/[...]/include/c++/7.3.0/ext/new_allocator.h(136):error:function"std::pair::pair(conststd::pair&)[with_T1=cons

c++ - constexpr std::optional 重置

我正在审查C++-17std::optional类模板的接口(interface),并注意到reset和assignment来自nullopt的未标记为constexpr。这是一个疏忽还是无法将此操作标记为constexpr的原因? 最佳答案 有一个原因,就是[expr.const]以前禁止:anassignmentexpressionorinvocationofanassignmentoperator([class.copy])thatwouldchangetheactivememberofaunion;由于P1330:Chang

c++ - 我可以使用 std::vector 作为预分配(原始)数组的外观吗?

我已经从DirectX获得了一个内存位置,其中存储了我的顶点信息。处理顶点信息的一种极其方便的方法是使用包含顶点信息的结构的std::vector。鉴于我有一个指向大缓冲区的指针,我可以使用std::vector来管理缓冲区中的元素吗?定期构造一个std::vector会导致它有自己的地址,这并不是我真正想要的。我能以某种方式使用新的运算符放置吗? 最佳答案 是的,你可以。使用customallocator.在此分配器中,您的DirectX内存的返回地址。这是一个基于CompellingexamplesofcustomC++STLa