我想使用lambda作为自定义比较器std::map但不幸的是Visual Studio 2013编译器不允许使用这样的简单代码:
auto cmp = [](int l, int r) { return l < r; };
std::map<int, int, decltype(cmp)> myMap(cmp);
myMap[1] = 1;
Run Code Online (Sandbox Code Playgroud)
并失败了
错误C3497:您无法构造lambda的实例
似乎此代码在GCC 5.1和Visual Studio 2015中正常工作(使用ideone和VC++在线编译器进行检查 ).但对于VS2013,其中一个解决方案是使用此处提出的参考(注意auto&):
auto& cmp = [](int l, int r) { return l < r; };
std::map<int, int, decltype(cmp)> myMap(cmp);
myMap[1] = 1;
Run Code Online (Sandbox Code Playgroud)
很明显,由于绑定非const引用到临时,GCC不编译这个,而VS2015发出关于使用非标准扩展的警告.也可以使用const引用,但是下面的代码将不会编译(注意可变 - 我通过使用有状态比较器来扩展它):
int compCounter = 0;
const auto& cmp = [&compCounter](int l, int r) mutable { ++compCounter; return …Run Code Online (Sandbox Code Playgroud) MSVC和clang/gcc不同意是否可以在三元运算符中使用两个不同的积分常数(因此它们是否具有a common_type):
#include <utility>
int main()
{
return false
? std::integral_constant<int, 1>()
: std::integral_constant<int, 2>();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码段在clang和gcc中编译得很好,但在MSVC中却没有.根据标准,正确的行为是什么?如果它是clang/gcc行为,那么用于推断这两种不同类型的常见类型的转换序列是什么?
使用以下方法编写将检测类型中特定成员的存在的模板很容易void_t:
#include <type_traits>
// This comes from cppreference
template<typename... Ts> struct make_void { typedef void type;};
template<typename... Ts> using void_t = typename make_void<Ts...>::type;
// primary template handles types that have no ::aMember
template< class T, class = void_t<> >
struct has_aMember : std::false_type { };
// specialization recognizes types that do have a ::aMember
template< class T >
struct has_aMember<T, void_t<decltype( T::aMember )>> : std::true_type { };
Run Code Online (Sandbox Code Playgroud)
现在,如果我想检测是否存在其他成员,我将不得不复制粘贴检测器模板,只需更改aMember为otherMember:
template< class T, class = void_t<> …Run Code Online (Sandbox Code Playgroud) 让我们说,A并且B是相同大小的矩阵.在Matlab,我可以使用如下的简单索引.
idx = A>0;
B(idx) = 0
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做OpenCV?我应该使用
for (i=0; ... rows)
for(j=0; ... cols)
if (A.at<double>(i,j)>0) B.at<double>(i,j) = 0;
Run Code Online (Sandbox Code Playgroud)
这样的事情?是否有更好(更快,更有效)的方式?
而且,在OpenCV我尝试的时候
Mat idx = A>0;
Run Code Online (Sandbox Code Playgroud)
变量idx似乎是一个CV_8U矩阵(不是布尔而是整数).
考虑测量执行时间和执行的交换次数的简单代码:
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>
struct A {
A(int i = 0) : i(i) {}
int i;
static int nSwaps;
friend void swap(A& l, A& r)
{
++nSwaps;
std::swap(l.i, r.i);
}
bool operator<(const A& r) const
{
return i < r.i;
}
};
int A::nSwaps = 0;
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
int main()
{
std::vector<A> v(10000000);
std::minstd_rand gen(std::random_device{}());
std::generate(v.begin(), v.end(), [&gen]() {return gen();});
auto s = high_resolution_clock::now();
std::sort(v.begin(), v.end());
std::cout << duration_cast<milliseconds>(high_resolution_clock::now() …Run Code Online (Sandbox Code Playgroud) 假设我有一些类层次结构,其中有几个virtual函数返回容器引用:
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
class Interface {
public:
virtual const std::vector<int>& getArray() const = 0;
virtual const std::set<int>& getSet() const = 0;
virtual const std::map<int, int>& getMap() const = 0;
};
class SubclassA : public Interface {
public:
const std::vector<int>& getArray() const override { return _vector; }
const std::set<int>& getSet() const override { return _set; }
const std::map<int, int>& getMap() const override { return _map; }
private:
std::vector<int> _vector;
std::set<int> _set; …Run Code Online (Sandbox Code Playgroud) 我对熊猫索引列的结果感到困惑。
都
db['varname']
Run Code Online (Sandbox Code Playgroud)
和
db[['varname']]
Run Code Online (Sandbox Code Playgroud)
给我'varname'的列值。但是,看起来有些细微的差别,因为的输出db['varname']显示了值的类型。