2011-11-07 45 views
2

有没有办法在C++中“批量评估”数组的内容?例如,让int数字[10] = {23,42,12,42,53,10,0,0,0,0}。有没有办法循环数组中的每个元素,并检查元素是否符合指定的条件?C++数组求值

总之,我希望能够做这样的事情:

if(every element in the array meets arbitrary condition) 
do this 

if(array element from m to array element n==something) 
do this 

对于小数组,我知道我可以使用类似:如果(号码[ 0] == blabla)& & if(numbers [n] == blabla),那么这样做,但显然这不是一个现实的解决方案,当评估非常大的数组。

+0

似乎你对编程有很好的理解,但对C++没有经验。我会寻找一本书或一套好的教程。确保它是为有编程经验的人编写的,所以你不会觉得无聊并跳过(重要)的东西。 –

回答

3

“如果(在阵列中的每个元素满足任意条件)执行此” 与STL:

bool IsOdd (int i) 
{ 
    return ((i%2)==1); 
} 
    //... 
{ 
    vector<int> myvector; 
    vector<int>::iterator it; 

    myvector.push_back(2); 
    myvector.push_back(4); 
    myvector.push_back(6); 
    myvector.push_back(8); 

    it = find_if (myvector.begin(), myvector.end(), IsOdd); 
    if (it == myvector.end()) 
    cout<< "No Odd numbers"; 
    } 

“如果(在阵列中的每个元素满足任意条件)执行此” 而不STL

numbers[10]={2,4,6,8,10,12,14,16,18,20} 
bool oddExist=false; 
for (int i =0;i<10;++i) 
    { 
    if (numbers[i]%2) 
    {      //added 
     oddExist=true;  
     break;    //added for efficiency, was not in 
    }      //  first post. 
    } 
     if (!oddExist) 
     cout<< "No Odd numbers"; 

“如果(数组元素从m到数组元素n ==东西)执行此” 与STL

void printNumber (int i) 
{ 
    cout << i; 
} 

    // ... 

    vector<int> myvector; 
    myvector.push_back(10); 
    myvector.push_back(20); 
    myvector.push_back(30); 
    myvector.push_back(40); 

    for_each (myvector.begin(), myvector.end(), printNumber); 

“如果(数组元素从M到数组元素n ==东西)这样做”不 STL

numbers[10]={2,4,6,8,10,12,14,16,18,20} 

for (int i =0;i<10;++i) 
    cout << numbers[i]; 
+1

良好的STL使用。不要忘记[BOOL平等(IA1,IA2,IB1,PRED);](http://www.sgi.com/tech/stl/equal.html)这可以用于第二种情况。 –

+0

@LokiAstari这是我从来没有使用过的。看起来很有趣。谢谢。 –

2

有没有办法循环访问数组中的每个元素并检查元素是否符合指定条件?

您可以使用std::for_each算法。

3

你可能是指“为”

for(int i = 0; i < size_of_array; i++) 
    if(the_condition_function(numbers[i])){ 
    //do this 
    } 
2

因为你的条件似乎是每一个元素满足条件,它可能是更有效地使用的算法,如std::findstd::find_if。对于find_if,你可以定义一个函子,当你的条件不满意时返回true,算法会在第一次出现时停止,而不是遍历整个数组。

0

只是为了competeness缘故

你也可以使用基于不等的,但是您的编译器必须已经实现了C++ 11版本的这一部分。我知道微软的Visual C++(2010)还没有。虽然我相信GCC 4+已经。

int my_array[] = {2,4,6,8,10,12,14,16,18,20}; 
for (int &x : my_array) { 
    std::cout << x << std::endl; 
} 

Alternativelly好东西是用一个lambda函数一起使用std :: for_each的,我发现msdn:在解释拉姆达的非常好。

int my_array[] = {2,4,6,8,10,12,14,16,18,20}; 
std::for_each(my_array[0], my_array[10], [&] (int* iter) { 
    std::cout << *iter << std::endl; 
}); 

或简单地如上所述的for语句。

0

如果(在阵列中的每个元素满足任意条件)
(来自米数组元素数组元素n ==东西)为此

if (std::find_if(&a[0], &a[size], testUnaryFunction) == &a[size]) 
{ 
    do this // if all elements in i => [0-size) testUnaryFunction(a[i]) return false 
} 

如果
要这样做

if (std::equal(&a[0], &a[size], &b[0], testBinaryFunctin)) 
{ 
    do this // if all elements in i => [0-size) testBinaryFunctin(a[i], b[i]) returns true 
} 

如果你有C++ 11,那么你可以使用闭包:

if (std::find_if(&a[0], &a[size], [](type const& val) { return val%2 == 0;}) == &a[size]) 
{ 
    do this // if all elements are odd 
} 

if (std::equal(&a[0], &a[size], &b[0], [](type const& lhs, type const& rhs) { return lhs == rhs;})) 
{ 
    do this // if arrays a and b are equal. 
}