ESP-IDF实现例程:
/5位选手,分别打十个分数,取中间8个分数,求平均值,然后根据选手的分数排序输出/
#include <stdio.h>
#include
using namespace std;
#include
#include
#include
#include
class player
{
public:
player(){};
player(string name, int score) : mName(name), mScore(score){};
public:
string mName;
int mScore;
};
// 选手初始化
void CreatePlayer(vector &v2)
{
string nameList = “ABCDE”;
for (int i = 0; i < 5; i++)
{
player p2;
p2.mName = “选手”;
p2.mName += nameList[i];
p2.mScore = 0;
v2.push_back(p2);
}
}
void printMyScore(int val) {
cout<< val << " ";
}
bool mCompare(player p1, player p2) {
return p1.mScore < p2.mScore;
}
// 给选手打分并排序
void SetScore(vector &v2)
{
for(vector::iterator it2 = v2.begin(); it2 != v2.end(); it2++) {
deque dScore;
for(int i=0; i<10; i++) {
int score = rand() % 40 + 60;
dScore.push_back(score);
}
sort(dScore.begin(),dScore.end());
dScore.pop_front();
dScore.pop_back();
for_each(dScore.begin(),dScore.end(),printMyScore);
cout<<endl;
int totalScore = 0;
// for (int i=0; i<dScore.size(); i++) {
// totalScore += dScore[i];
// }
for (deque::iterator itscore = dScore.begin(); itscore != dScore.end(); itscore++) {
totalScore += (*itscore);
}
int mAverage = totalScore / dScore.size();
(*it2).mScore = mAverage;
}
sort(v2.begin(),v2.end(),mCompare);
}
// 打印分数
void PrintPlayer(vector &v2)
{
for (vector::iterator it2 = v2.begin(); it2 != v2.end(); it2++)
{
cout << “选手名字” << (*it2).mName << endl;
// printf(“选手名字%s\n”, (*it2).mName.c_str());
printf(“选手分数%d\n”, (*it2).mScore);
}
}
extern “C” void app_main(void)
{
/5位选手,打十个分数,取中间8个分数,求平均值然后排序输出/
std::vector v2;
CreatePlayer(v2);
SetScore(v2);
PrintPlayer(v2);
}
结果:
下面是官方的例程笔记