STL.cpp,RUN
//STL容器 http://www.cplusplus.com/reference/stl/
//STL算法 http://www.cplusplus.com/reference/algorithm
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
bool Compare(char a, char b)
{
return a > b;
}
void BasicTest()
{
std::string str;
std::cin >> str;
//http://www.cplusplus.com/reference/algorithm/sort/
std::sort(str.begin(), str.end());//字符串从小到大
std::cout << str << std::endl;
std::sort(str.begin(), str.end(), Compare);//根据Compare定义排序
std::cout << str << std::endl;
//std::vector的排序
std::vector<std::string> vString;
int n;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::string str;
std::cin >> str;
vString.push_back(str);
}
std::sort(vString.begin(), vString.end());
//http://www.cplusplus.com/reference/algorithm/find/
std::vector<std::string>::iterator p;
p = std::find(vString.begin(), vString.end(), "test");//查找
}
class Student
{
private:
int age;
double score;
public:
Student(int a, double s) : age(a), score(s)
{
}
bool operator<(const Student &stu)
{
if (age < stu.age)return true;
if (age == stu.age && score < stu.score)return true;
return false;
}
friend bool CompareByScore(const Student& stu1, const Student& stu2);
};
//定义小于关系
bool CompareByScore(const Student& stu1, const Student& stu2)
{
if (stu1.score < stu2.score)return true;
if (stu1.score == stu2.score && stu1.age < stu2.age)return true;
return false;
}
void TestAdvacedSort()
{
std::vector<Student> students;
students.push_back(Student(18, 3.5));
students.push_back(Student(19, 3.3));
//用Student定义的operator<排序,从小到大
std::sort(students.begin(), students.end());
//用给定的CompareByScore排序,从小到大
std::sort(students.begin(), students.end(), CompareByScore);
}
int main()
{
BasicTest();
TestAdvacedSort();
return 0;
}