#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <iomanip>
using namespace std;
bool isOdd(int n)
{
cout << "HELLO";
bool val = ((n%2) == 1);
cout << val << endl;
return val;
}
int main()
{
srand(time(NULL));
vector<int> myVec;
myVec.reserve(20);
for(int i = 0; i < 20; i++)
{
myVec[i] = rand() % 100;
cout << myVec[i] << " ";
}
vector<int>::iterator q = std::find_if(myVec.begin(), myVec.end(), isOdd);
cout << setw(3) << "first odd = " << *q << endl;
}
위의 프로그램은 항상 벡터의 첫 번째 숫자를 출력하고 isOdd 함수는 사용하지 않습니다. 나는에서라도 usinf 오전 g++ odd.cpp
find_if 항상 첫 번째 요소를 제공하고 isOdd 함수에 들어 가지 않음
를 사용하여 프로그램을 컴파일하는 것은
가 오'myVec'가 비어있다! 아마도'reserve' 대신에'resize'를 쓰려고했을 것입니다. – Rakete1111
또는'myVec.emplace_back (rand() % 100);'. –
벡터가 비어 있지 않습니다. 벡터 요소를 인쇄하고 있습니다. –