1 字符排序
#include <iostream>
#include <algorithm> // for std::sort
#include <string>
int main() {
std::string str = "programming";
// 对字符串中的字符进行排序
std::sort(str.begin(), str.end());
std::cout << "排序后的字符串: " << str << std::endl;
// 输出: aggimmnoprr
return 0;
}
2 学生成绩系统 multimap

#include <iostream>
#include <map>
#include <vector>
#include <string>
int main() {
std::multimap<std::string, std::pair<std::string, int>> studentRecords;
// 添加学生记录(班级,{姓名,分数})
studentRecords.insert({"ClassA", {"Alice", 85}});
studentRecords.insert({"ClassB", {"Bob", 90}});
studentRecords.insert({"ClassA", {"Charlie", 78}});
studentRecords.insert({"ClassA", {"David", 92}});
// 查询ClassA的所有学生
std::cout << "Students in ClassA:" << std::endl;
auto range = studentRecords.equal_range("ClassA");
for (auto it = range.first; it != range.second; ++it) {
std::cout << it->second.first << ": " << it->second.second << std::endl;
}
return 0;
}
浙公网安备 33010602011771号