C++ 17通过使用可选的ExecutionPolicy参数(作为第一个参数)升级了69个STL算法以支持并行性.例如.
std::sort(std::execution::par, begin(v), end(v));
Run Code Online (Sandbox Code Playgroud)
我怀疑C++ 17标准故意没有说明如何实现多线程算法,而是由图书馆作者决定什么是最好的(并允许他们改变主意,稍后).尽管如此,我仍然希望在高层次上理解在并行STL算法的实现中正在考虑哪些问题.
我心中的一些问题包括(但不限于!):
我意识到这些并行算法的目的是保护程序员不必担心这些细节.但是,任何能让我高度了解图书馆内部内容的精彩信息的信息都将受到赞赏.
这可能是STL中最糟糕的命名功能吗?(修辞问题)
std :: remove_copy_if()实际上似乎没有做任何删除.我可以说,它的行为更像是copy_if_not.
否定有点令人困惑,但可以解决std :: not1(),但是我可能误解了一些东西,因为我无法理解这个函数与删除有什么关系 - 我错过了什么?
如果没有,是否有一个STL算法用于有条件地从容器中移除(移动?)元素并将它们放入另一个容器中?
编辑以添加示例,以便读者不那么困惑.
以下程序似乎保持输入范围(V1)不变:
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using std::cout;
using std::endl;
int main (void)
{
std::vector<int> V1, V2;
V1.push_back(-2);
V1.push_back(0);
V1.push_back(-1);
V1.push_back(0);
V1.push_back(1);
V1.push_back(2);
std::copy(V1.begin(), V1.end(), std::ostream_iterator<int>(cout, " "));
cout << endl;
std::remove_copy_if(
V1.begin(),
V1.end(),
std::back_inserter(V2),
std::bind2nd(std::less<int>(), 0));
std::copy(V2.begin(), V2.end(), std::ostream_iterator<int>(cout, " "));
cout << endl;
std::copy(V1.begin(), V1.end(), std::ostream_iterator<int>(cout, " "));
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
它输出:
-2 0 -1 0 1 2
0 0 1 …
Run Code Online (Sandbox Code Playgroud) 在g ++ 4.6(或更高版本)中,除了-ffast-math之外还有什么额外的优化功能?
手册页说这个选项"还支持对所有标准兼容程序无效的优化".我在哪里可以找到有关这是否会影响我的程序的更多信息?
是否有任何方法可以使GNU make的并行调用(即make -jN)在遇到错误时立即停止所有编译?
目前,我看到"等待未完成的工作"消息,然后在现有的制作流程完成时输出了许多行.
当我用gcc-4.6.3或gcc-4.7.2编译这个程序时,编译器给出了一个关于重载调用不明确的错误:
#include <iostream>
#include <functional>
class Scott
{
public:
void func(const bool b = true)
{
std::cout << "Called func() with a boolean arg" << std::endl;
}
void func(std::function<void(void)> f)
#ifdef WITH_CONST
const
#endif
{
std::cout << "Called func() with a std::function arg" << std::endl;
}
};
int main (int argc, char *argv[])
{
Scott s;
s.func([] (void) { });
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使重载函数const,它编译很好并调用我没想到的方法!
devaus120>> g++ -Wall -std=c++11 -DWITH_CONST wtf.cxx
devaus120>> ./a.out
Called func() with a boolean arg
Run Code Online (Sandbox Code Playgroud)
所以,我有两个问题:
TIA. …
我想创建一个动态函数,在声明函数时使用(计算?)变量的值.
下面的示例要求$ var作为全局变量存在,因此可以在调用函数时使用它:
my $var = 'something';
someFunction(sub { return $_[0] eq $var; });
Run Code Online (Sandbox Code Playgroud)
但我猜有一些方法来创建动态函数,所以它被声明如下:
someFunction(sub { return $_[0] eq 'something'; });
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点!?:)
std :: bind的返回类型是(有意)未指定的.它可以存储在std :: function中.
下面的示例程序显示了如何将std :: bind()返回的临时对象显式转换为std :: function以调用fn1().
如果std :: bind的返回类型是可知的,我可以重载Callback构造函数并且不再需要显式地转换std :: bind临时对象.
有没有办法避免显式演员?
// g++ -std=c++11 test.cxx
#include <functional>
using std::placeholders::_1;
class A
{
public:
void funcA (int x) { }
};
class Callback
{
public:
Callback () = default;
Callback (std::function<void(int)> f) { }
// Wish we knew the return type of std::bind()
// Callback (return_type_of_std_bind f) { }
};
void fn0 (std::function<void(int)> f) { }
void fn1 (Callback cb) { }
int main …
Run Code Online (Sandbox Code Playgroud) c++ ×5
stl ×3
c++11 ×2
concurrency ×1
g++ ×1
gcc ×1
gnu-make ×1
makefile ×1
optimization ×1
overloading ×1
performance ×1
perl ×1
std-function ×1
stdbind ×1