C++有关vector算法的使用

#include "stdafx.h"
#include "FGConfig.h"
#include "Utils.h"
#include "persistable.h"
#include <iostream>
#include <string>
using namespace std;
using namespace xml_api;

#include <crtdbg.h>
inline void EnableMemLeakCheck()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

 

class KeyPair
{
public:
string m_strName;
int m_nValue;
};

void ShowAllDatas( vector<KeyPair> &datas )
{
vector<KeyPair>::iterator itor = datas.begin();

while(itor != datas.end())
{
cout << itor->m_strName << " " << itor->m_nValue << endl;
++itor;
}
}

bool EraseDataByString(vector<KeyPair> &datas , string name)
{
vector<KeyPair>::iterator itor = datas.begin();
while(itor != datas.end())
{
if(name == itor->m_strName)
{
datas.erase(itor);
return true;
}
++itor;
}

return false;
}

int _tmain(int argc, _TCHAR* argv[])
{

 

vector<KeyPair> datas;
KeyPair key;
key.m_strName = "c++";
key.m_nValue = 29.5;
datas.push_back(key);


key.m_strName = "c";
key.m_nValue = 99;
datas.push_back(key);

ShowAllDatas(datas);
cout << "*****************************************" << endl;
if (EraseDataByString(datas, "c"))
{
ShowAllDatas(datas);
}
}

posted @ 2013-07-09 19:47  Predator  阅读(291)  评论(0编辑  收藏  举报