📄 vector.cpp
字号:
#pragma warning(disable:4786)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std ;
// ID 类用于小组记分,它保存了每一个选手的名字和积分
class ID
{
public:
string Name;
int Score;
ID() : Name(""), Score(0) {}
ID(string NewName, int NewScore) : Name(NewName), Score(NewScore) {}
};
// 在这个例子中只有名字和积分同时一样才认为两个ID是相同的。
bool operator==(const ID& x, const ID& y)
{
return (x.Name == y.Name) && (x.Score == y.Score);
}
//按照积分排序
bool operator<(const ID& x, const ID& y)
{
return x.Score < y.Score;
}
//定义一个ID向量的模板类。
typedef vector<ID> NAMEVECTOR;
int main()
{
//声明一个可动态分配的ID向量
NAMEVECTOR theVector;
// 声明一个迭代器
NAMEVECTOR::iterator theIterator;
//初始化ID向量
theVector.push_back(ID("Karen Palmer", 2));
theVector.push_back(ID("Ada Campbell", 1));
theVector.push_back(ID("John Woloschuk", 3));
theVector.push_back(ID("Grady Leno", 2));
cout << "Players and scores:" << endl;
//输出原来的选手名字及积分
for (theIterator = theVector.begin(); theIterator != theVector.end();
theIterator++)
cout << theIterator->Score << " "
<< theIterator->Name << endl;
cout << endl;
// 调用sort算法排序
sort(theVector.begin(), theVector.end());
cout << "Players ranked by score:" << endl;
for (theIterator = theVector.begin(); theIterator != theVector.end();
theIterator++)
cout << theIterator->Score << " "
<< theIterator->Name << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -