#include <list>
#include <algorithm>
#include "windows.h"#pragma warning (disable : 4786)#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
#include <string>struct Mystruct
{
Mystruct(int id, const char* name,int num)
:id(id), name(name) ,number( num)
{
}
int id;
string name;
int number;
};
int main(int argc, char* argv[])
{
list <Mystruct> MyList;
MyList.push_back(Mystruct(1, "Allan",23));
MyList.push_back(Mystruct(1, "Alln",2323));
MyList.push_back(Mystruct(1, "Ald",233));
MyList.push_back(Mystruct(2, "Emily",2));
MyList.push_back(Mystruct(3, "Andrew",23)); 想弄一个函数把ID为1的 number求和(当然ID要动态变化)
return 0;
}
#include <algorithm>
#include "windows.h"#pragma warning (disable : 4786)#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
#include <string>struct Mystruct
{
Mystruct(int id, const char* name,int num)
:id(id), name(name) ,number( num)
{
}
int id;
string name;
int number;
};
int main(int argc, char* argv[])
{
list <Mystruct> MyList;
MyList.push_back(Mystruct(1, "Allan",23));
MyList.push_back(Mystruct(1, "Alln",2323));
MyList.push_back(Mystruct(1, "Ald",233));
MyList.push_back(Mystruct(2, "Emily",2));
MyList.push_back(Mystruct(3, "Andrew",23)); 想弄一个函数把ID为1的 number求和(当然ID要动态变化)
return 0;
}
// 二元函数对象
struct calculator : binary_function<int, Mystruct, int>
{
int operator()(const int _left, const Mystruct &_right)
{
if (_right.id == 1) // 过滤条件
{
return _left + _right.number;
}
else
{
return _left;
}
}
};// 调用
int sum = accumulate(MyList.begin(), MyList.end(), 0, calculator());
#include <algorithm>
#include <windows.h>
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
#include <string>
using namespace std;struct Mystruct
{
Mystruct(int id, const char* name,int num)
: id(id), name(name) ,number( num)
{
}
int id;
string name;
int number;
}; struct Sum
{
Sum()
: iSum(0)
{
} template <class InputIterator>
int operator() (InputIterator BeginIter, InputIterator EndIter, int ID)
{
for (; BeginIter != EndIter; ++BeginIter)
{
if (BeginIter->id == ID)
{
iSum += BeginIter->number;
}
} return iSum;
}private:
int iSum;
};int main(int argc, char* argv[])
{
list <Mystruct> MyList; MyList.push_back(Mystruct(1, "Allan",23));
MyList.push_back(Mystruct(1, "Alln",2323));
MyList.push_back(Mystruct(1, "Ald",233));
MyList.push_back(Mystruct(2, "Emily",2));
MyList.push_back(Mystruct(3, "Andrew",23)); int sum = Sum()(MyList.begin(), MyList.end(), 1); return 0;
}VS2008下编译通过
#include <list>
#include <algorithm>
#include <windows.h>
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
#include <string>
using namespace std;struct Mystruct
{
Mystruct(int id, const char* name,int num)
: id(id), name(name) ,number( num)
{
}
int id;
string name;
int number;
}; struct Sum
{
Sum()
: iSum(0)
{
} template <class InputIterator>
int operator() (InputIterator BeginIter, InputIterator EndIter, int ID)
{
for (; BeginIter != EndIter; ++BeginIter)
{
if (BeginIter->id == ID)
{
iSum += BeginIter->number;
}
} return iSum;
}private:
int iSum;
};int main(int argc, char* argv[])
{
list <Mystruct> MyList; MyList.push_back(Mystruct(1, "Allan",23));
MyList.push_back(Mystruct(1, "Alln",2323));
MyList.push_back(Mystruct(1, "Ald",233));
MyList.push_back(Mystruct(2, "Emily",2));
MyList.push_back(Mystruct(3, "Andrew",23)); int sum = Sum()(MyList.begin(), MyList.end(), 1); return 0;
}
struct calculator : binary_function<int, Mystruct, int>
{
calculator(int filterID) : m_filterID(filterID)
{ } int operator()(const int _left, const Mystruct &_right)
{
if (_right.id == m_filterID)
{
return _left + _right.number;
}
else
{
return _left;
}
} int m_filterID;
};int sum = accumulate(MyList.begin(), MyList.end(), 0, calculator(1));
struct calculator : binary_function<int, Mystruct, int>
{
calculator(int filterID) : m_filterID(filterID)
{ } int operator()(const int _left, const Mystruct &_right)
{
if (_right.id == m_filterID)
{
return _left + _right.number;
}
else
{
return _left;
}
} int m_filterID;
};int sum = accumulate(MyList.begin(), MyList.end(), 0, calculator(1));