我是std :: thread的新手,我尝试编写代码parallel_for.我编写了以下内容:
// parallel_for.cpp
// compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread
// execution: time ./parallel_for 100 50000000
// (100: number of threads, 50000000: vector size)
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <thread>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
// Parallel for
template<typename Iterator, class Function>
void parallel_for(const Iterator& first, const Iterator& last, Function&& f, const int nthreads = 1, const int threshold = 1000)
{
const unsigned int group = …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#include <iostream>
#include <type_traits>
// Variadic version
template<class... Variadic>
void f(const Variadic&... variadic)
{
std::cout<<"variadic"<<std::endl;
}
// Single version
template<class Single, class = typename std::enable_if<std::is_fundamental<Single>::value>::type>
void f(const Single& single)
{
std::cout<<"single"<<std::endl;
}
// Main
int main()
{
f(); // variadic
f(42); // single : why?
f(std::string()); // variadic
f(42, 42); // variadic
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么标记为"单一"的行编译得很好(在g ++ 4.6.3下)并且不会产生重载解析问题.c ++ 11标准是否说具有固定数量参数的模板函数优先于可以具有相同签名的可变参数函数?
c++ standards-compliance overload-resolution variadic-templates c++11
考虑到功能:
template <class T> void f(const T* const ptr);
Run Code Online (Sandbox Code Playgroud)
什么是T对f(nullptr)?
考虑以下设计:
template <class SecondType>
struct First
{
SecondType* _ptr;
};
template <class FirstType>
struct Second
{
FirstType* _ptr;
};
Run Code Online (Sandbox Code Playgroud)
其中First类型有一个指向类型的指针,Second反之亦然。问题是我不能声明这一点,因为它们是相互依赖的,我应该声明First<Second<First<Second...>>>.
如何解决这个问题呢 ?
考虑来自Bit Twiddling Hacks网站的这个链接.为了计算尾随位,使用以下算法:
unsigned int v; // 32-bit word input to count zero bits on right
unsigned int c = 32; // c will be the number of zero bits on the right
v &= -signed(v); /* THIS LINE */
if (v) c--;
if (v & 0x0000FFFF) c -= 16;
if (v & 0x00FF00FF) c -= 8;
if (v & 0x0F0F0F0F) c -= 4;
if (v & 0x33333333) c -= 2;
if (v & 0x55555555) c -= 1; …Run Code Online (Sandbox Code Playgroud) 考虑用二进制编写的两个数字(左边是MSB):
X = x7 x6 x5 x4 x3 x2 x1 x0
Run Code Online (Sandbox Code Playgroud)
和
Y = y7 y6 y5 y4 y3 y2 y1 y0
Run Code Online (Sandbox Code Playgroud)
这些数字可以具有任意数量的位,但两者的类型相同.现在考虑x7 == y7,x6 == y6,x5 == y5,但x4 != y4.
如何计算:
Z = x7 x6 x5 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
或换句话说,如何有效地计算一个数字,使公共部分保持在最后一个不同位的左边?
template <typename T>
inline T f(const T x, const T y)
{
// Something here
}
Run Code Online (Sandbox Code Playgroud)
例如,对于:
x = 10100101
y = 10110010
Run Code Online (Sandbox Code Playgroud)
它应该回来
z = 10100000
Run Code Online (Sandbox Code Playgroud)
注意:它用于超级计算,此操作将执行数十亿次,因此应该避免逐个扫描位...
考虑std::vector<std::pair<int, int>>基于对的第一个元素的比较进行排序.
现在假设我申请:
std::unique(std::begin(v),
std::end(v),
[](const std::pair<int, int>& x, const std::pair<int, int>& y)
{return x.first == y.first;});
Run Code Online (Sandbox Code Playgroud)
我是否有保证std::unique每个相等范围的第一个元素?
考虑一个类:
struct mystruct
{
constexpr operator char() {return x;}
signed char x;
};
Run Code Online (Sandbox Code Playgroud)
和一个像这样的操作:
mystruct m;
m.x = /* something at runtime */
int i = 3 * m + 45ULL * m;
Run Code Online (Sandbox Code Playgroud)
编译器是否能够跳过临时转换char并直接转换为表达式中m所需的类型3 * m + 45ULL * m?
我想知道为什么std::bitset::reference并std::vector<bool>::reference指定一个显式析构函数(不是编译生成的析构函数).因为,例如,boost::dynamic_bitset::reference似乎没有指定这样的析构函数.
考虑以下帮助器结构:
template <class T>
struct bit_count_1:
std::integral_constant<
std::size_t,
std::numeric_limits<typename std::make_unsigned<T>::type>::digits
> {};
template <class T>
struct bit_count_2:
std::integral_constant<
std::size_t,
std::numeric_limits<T>::digits + std::is_signed<T>::value
> {};
template <class T>
constexpr std::size_t compute_bit_count() {
using type = typename std::make_unsigned<T>::type;
constexpr type zero = 0;
constexpr type one = 1;
constexpr type max = ~zero;
type current = max;
std::size_t i = 0;
while (current) {
current >>= one;
++i;
}
return i;
}
template <class T>
struct bit_count_3:
std::integral_constant<
std::size_t,
compute_bit_count<T>()
> {}; …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×8
algorithm ×2
standards ×2
templates ×2
bit ×1
c ×1
c++14 ×1
casting ×1
class ×1
destructor ×1
integer ×1
nullptr ×1
optimization ×1
reference ×1
std-bitset ×1
std-function ×1
stdthread ×1
unique ×1