C++ map iterator如何比较两个迭代器

   2024-10-02 7840
核心提示:在C++中,可以使用==运算符来比较两个map迭代器是否指向相同的元素。例如:std::mapint, std::string myMap = {{1, apple}, {2,

在C++中,可以使用==运算符来比较两个map迭代器是否指向相同的元素。例如:

std::map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "cherry"}};auto it1 = myMap.find(2);auto it2 = myMap.find(3);if (it1 == it2) {    std::cout << "it1 and it2 point to the same element" << std::endl;} else {    std::cout << "it1 and it2 point to different elements" << std::endl;}

上面的代码会输出"it1 and it2 point to different elements",因为it1指向键为2的元素,而it2指向键为3的元素。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号