std::swap 【chatgpt】

std::swap  map变量数据交换....
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, std::string> myMap;

    // 添加元素
    myMap["key1"] = "value1";
    myMap["key2"] = "value2";
    myMap.insert(std::make_pair("key3", "value3"));

    // 打印所有元素
    for (const auto& pair : myMap) {
        std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
    }

    // 修改元素值
    myMap["key2"] = "new value2";

    // 删除元素
    myMap.erase("key3");

    // 查找元素
    auto it = myMap.find("key1");
    if (it != myMap.end()) {
        std::cout << "Value of 'key1': " << it->second << std::endl;
    } else {
        std::cout << "Key 'key1' not found." << std::endl;
    }

    return 0;
}

  

posted on 2023-08-09 07:45  lydstory  阅读(15)  评论(0编辑  收藏  举报

导航