我正在使用两台计算机,每台计算机都有不同版本的visual studio.在visual studio 2008计算机上,我的代码编译.在visual 2010计算机上,我的代码没有编译,因为我正在使用宏max(a,b),据我所知,这是在stdlib.h中定义的.我不能只定义max(a,b)因为它将是对visual 2008计算机的重新定义.但是如果我没有定义max(a,b)我的代码就不能在visual 2010计算机上编译.
有解决方案吗
有很多资源描述了malloc和之间的功能差异calloc,但我不能轻易找到描述不同功能签名背后的历史的资源:
void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
Run Code Online (Sandbox Code Playgroud)
当然,size前者是每个成员的大小.也许这个想法是可以通过操作系统懒散地完成多页面大小的成员大小的calloc?
(我可以弥补原因以及下一个人 - 没有引用来源没有接受的答案.:-)
什么是迭代std::set/ std::multiset/ std::map/ 的时间复杂度std::multimap?我相信它在集合/地图的大小上是线性的,但不是那么肯定.是否在语言标准中指定?
考虑以下:
std::ostream out(nullptr);
Run Code Online (Sandbox Code Playgroud)
这是合法的,定义明确吗?
如果我现在这样做:
out << "hello world\n";
Run Code Online (Sandbox Code Playgroud)
这是合法的,定义明确吗?如果是这样的话,大概这是一种无操作?
今天,我偶然发现了构造函数的这些标准声明std::vector:
// until C++14
explicit vector( const Allocator& alloc = Allocator() );
// since C++14
vector() : vector( Allocator() ) {}
explicit vector( const Allocator& alloc );
Run Code Online (Sandbox Code Playgroud)
在大多数标准容器中都可以看到这种变化.一个稍微不同的例子是std::set:
// until C++14
explicit set( const Compare& comp = Compare(),
const Allocator& alloc = Allocator() );
// since C++14
set() : set( Compare() ) {}
explicit set( const Compare& comp,
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
两种模式之间有什么区别?它们(dis)的优势是什么?
它们是否完全等效 - 编译器是否生成类似于第一个的第二个类似的东西?
C++库中是否有任何实现的方法允许您对两个向量(当然大小和类型相同)的值求和?
例如:
std::vector<int> a;//looks like this: 2,0,1,5,0
std::vector<int> b;//looks like this: 0,0,1,3,5
Run Code Online (Sandbox Code Playgroud)
现在,将它们的值一起添加应该如下所示:
//2,0,2,8,5
Run Code Online (Sandbox Code Playgroud)
我期待的答案是"没有"或"是"+方法.
为了更熟悉C++ 17,我刚才注意到std::visit:
template <class Visitor, class... Variants>
constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars);
Run Code Online (Sandbox Code Playgroud)
为什么不std::visit采用单一变体,而是采用任意数量的变体?我的意思是,你总是可以采用一些标准的库函数,让它采用具有相同角色的多个参数,对所有这些参数进行处理(例如,std::find()对于容器中的多个元素); 或者你可以带多个访问者并在同一个变体上使用它们.
那么,为什么这个特定的"变化"?
似乎有一个问题,使用C++中的文字istd::complex.
请考虑以下代码:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
Run Code Online (Sandbox Code Playgroud)
第二行无法编译:
error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
在函数调用中使用复杂文字时也会出现这种情况,例如
std::exp<std::complex<double>>( 1.0i * 3.14159 );
Run Code Online (Sandbox Code Playgroud)
为什么复杂的字面值1.0i不能转换为std::complex<double>?
我必须明确地构建一个std::complex与1.0i?
有些情况下,与启动STL一些功能make_前缀一样std::make_pair,std::make_shared,std::make_unique等它为什么一个更好的做法是使用它们,而不是简单地使用构造?
auto pair2 = std::pair< int, double >( 1, 2.0 );
auto pair3 = std::make_pair( 1, 2.0 );
std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) );
std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 );
Run Code Online (Sandbox Code Playgroud)
根据cppreference,该特性std::is_literal_type在C++ 17中已被弃用.问题是为什么以及什么是未来的首选替代品来检查类型是否是文字类型.