爲什麼下面的代碼對於bool的std :: vector不起作用(如同被忽略)? 某些元素在bool向量中是隨機錯誤的。 對於int向量都可以正常工作(用多於3個循環測試)。C++ OpenMP和std :: vector <bool>
我在ubuntu 14.04 64位g ++ 4.8.4。
#include <iostream>
#include <vector>
using namespace std;
class TestBool
{
public:
TestBool() {}
bool test() {return true;}
int testInt() {return 10;}
};
void testBVec(vector<bool> &bv, size_t loop)
{
cout << "boolvec loop " << loop << endl;
for(size_t i = 0; i < bv.size(); i++) {
if(! bv[i])
cout << "wholy shit with bool at index " << i << endl;
}
}
void testIntVec(vector<int> &iv, size_t loop)
{
cout << "intVec loop " << loop << endl;
for(size_t i = 0; i < iv.size(); i++) {
if(iv[i] != 10)
cout << "wholy shit with int at index " << i << endl;
}
}
int main()
{
vector<TestBool> tv(10);
size_t loops = 3;
for(size_t i = 0; i < loops; i++) {
vector<bool> bv(10);
vector<int> iv(10);
#pragma omp parallel for
for(int j = 0; j < 10; ++j) {
bv[j] = tv[j].test();
iv[j] = tv[j].testInt();
}
testBVec(bv, i+1);
testIntVec(iv, i+1);
}
return 0;
}
https://isocpp.org/blog/2012/11/on-vectorbool – Mansuro
感謝您的鏈接。 –