//h
#pragma once
#include <vector>
#include <set>
#include <string>
using namespace std;
typedef struct __strClient{
string strName;
int iNum;
bool bSex;
int iCell;
}strClient ,*pStrClient;
class CTemplate{
public:
CTemplate(void);
~CTemplate(void);
private:
std::vector<strClient> clients;
public:
int GetCellCount(); //单元数
int GetClientCountInCell(int); //一个单元里的人数
void CreateClient(int num,bool sex,string name, int iCell);
strClient& GetClientInfo(int); //用户信息
};
//cpp
#include "StdAfx.h"
#include "Template.h"
CTemplate::CTemplate(void)
{
}
int CTemplate::GetClientCountInCell(int cellNum)
{
int num =0;
vector<strClient>::iterator it;
for (it=clients.begin();it!=clients.end();it++)
{
if (it->iCell == cellNum)
{
num++;
}
}
return num;
}
void CTemplate::CreateClient(int num,bool sex,string name,int iCell)
{
strClient cli;
cli.iNum= num;
cli.bSex = sex;
cli.iCell = iCell;
cli.strName = name;
clients.push_back(cli);
}
int CTemplate::GetCellCount()
{
int num =0;
vector<strClient>::iterator iter;
set<int> strSet;
pair<set<int>::iterator, bool> p;
for(iter=clients.begin();iter != clients.end();iter++)
{
p = strSet.insert(iter->iCell);
if (p.second)
{
num++;
}
}
return num;
}
strClient& CTemplate::GetClientInfo(int userNum)
{
vector<strClient>::iterator iter;
for (iter = clients.begin();iter != clients.end();iter++)
{
if (iter->iNum = userNum)
{
return *iter;
}
}
}
CTemplate::~CTemplate(void)
{
}
//main
#include "stdafx.h"
#include "Template.h"
int _tmain(int argc, _TCHAR* argv[])
{
int num1 = 101,num2 = 102,num3=103, num4=104,\
num5 = 201, num6=202,num7=203,num8=204,num9=205,\
num10 = 301,num11=302,num12=303, num13=304, num14=305;
string name1="wozai101",name2 = "wozai102",name3 = "wozai103",name4 = "wozai104",\
name5="wozai201",name6="wozai202",name7="wozai203",name8="wozai204",name9="wozai205",\
name10="wozai301",name11="wozai302",name12="wozai303",name13="wozai304",name14="wozai305";
bool sex1=true,sex2=false,sex3=true,sex4=true,\
sex5=false,sex6=true,sex7=true,sex8=false,sex9=true,\
sex10=true,sex11=true,sex12=false,sex13=true,sex14=true;
int cell1 = 10, cell2=10, cell3=10,cell4=10,\
cell5=20,cell6=20,cell7=20,cell8=20,cell9=20,\
cell10=30,cell11=30,cell12=30,cell13=30,cell14=30;
CTemplate ct;
ct.CreateClient(num1,sex1,name1,cell1);
ct.CreateClient(num2,sex2,name2,cell2);
ct.CreateClient(num3,sex3,name3,cell3);
ct.CreateClient(num4,sex4,name4,cell4);
ct.CreateClient(num5,sex5,name5,cell5);
ct.CreateClient(num6,sex6,name6,cell6);
ct.CreateClient(num7,sex7,name7,cell7);
ct.CreateClient(num8,sex8,name8,cell8);
ct.CreateClient(num9,sex9,name9,cell9);
ct.CreateClient(num10,sex10,name10,cell10);
ct.CreateClient(num11,sex11,name11,cell11);
ct.CreateClient(num12,sex12,name12,cell12);
ct.CreateClient(num13,sex13,name13,cell13);
ct.CreateClient(num14,sex14,name14,cell14);
int tmp = ct.GetClientCountInCell(20);
int tmp2 = ct.GetCellCount();
strClient tmp_user = ct.GetClientInfo(num13);
string tmp_name = tmp_user.strName;
printf("sum usersNumInCell = %d, cellCountInBuild=%d,user_name=%s\n",tmp,tmp2,tmp_name.c_str());
system("pause");
return 0;
}
stl 统计一栋楼里的信息
最新推荐文章于 2020-12-03 12:43:35 发布