std::pair的使用
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 6 class CellTrpPairInfo { 7 public: 8 CellTrpPairInfo() { 9 trpPairList_.clear(); 10 } 11 12 ~CellTrpPairInfo() { 13 trpPairList_.erase(trpPairList_.begin(), trpPairList_.end()); 14 } 15 16 void AddTrpPairInfo(const std::pair<int, int> &trpPair) { 17 trpPairList_.push_back(trpPair); 18 } 19 20 void printfTrpPairInfo() { 21 for (const auto &pair : trpPairList_) { 22 std::cout << pair.first << "->" << pair.second << endl; 23 } 24 } 25 26 std::vector<std::pair<int, int>> getTrpPairList() { 27 return trpPairList_; 28 } 29 30 void checkTrpPairInfo(const std::vector<std::pair<int, int>> &expectTrpPair) { 31 std::vector<std::pair<int, int>> actualTrpPair = getTrpPairList(); 32 std::sort(actualTrpPair.begin(), actualTrpPair.end(), [](std::pair<int, int> p1, std::pair<int, int> p2) 33 { return p1.first < p2.first; }); 34 std::cout << "checkTrpPairInfo:" << endl; 35 for (unsigned int i = 0; i < expectTrpPair.size(); i++) { 36 if (expectTrpPair[i].first == actualTrpPair[i].first && 37 expectTrpPair[i].second == actualTrpPair[i].second) { 38 std::cout << expectTrpPair[i].first << "->" << expectTrpPair[i].second << endl; 39 } else { 40 std::cout << "check failed!" << endl; 41 return; 42 } 43 } 44 return; 45 } 46 47 private: 48 std::vector<std::pair<int, int>> trpPairList_; 49 }; 50 51 int main() 52 { 53 CellTrpPairInfo *cellInfo = new CellTrpPairInfo(50); 54 std::vector<std::pair<int, int>> vec = {{1, 4}, {0, 3}, {2, 5}}; 55 for (const auto &pair : vec) { 56 cellInfo->AddTrpPairInfo(pair); 57 } 58 cellInfo->printfTrpPairInfo(); 59 std::vector<std::pair<int, int>> vec1 = {{0, 3}, {1, 4}, {2, 5}}; 60 cellInfo->checkTrpPairInfo(vec1); 61 delete cellInfo; 62 system("pause"); 63 return 0; 64 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具