首页 > 试题广场 >

以下 C++ 程序的输出结果的是() #include i

[单选题]
以下 C++ 程序的输出结果的是()
#include <iostream>
#include <map>
#include <memory>

int main(int argc, const char * argv[]) {
    
    std::map<int, std::shared_ptr<int>> map;
    std::shared_ptr<int> value1 = std::make_shared<int>(1);
    map[1] = value1;

    std::shared_ptr<int> value2 = std::make_shared<int>(2);
 
    value1.swap(value2);
    
    printf("%d\n", *value1);
    printf("%d\n", *value2);
    
    auto value3 = map[1];
    printf("%d\n", *value3);
    
    return 0;
}
  • 1
    2
    1
  • 1
    2
    2
  • 2
    1
    2
  • 2
    1
    1
map[1]存储的是value1的拷贝,指向1,value1也指向1,交换的只是value1

发表于 2025-06-15 20:44:27 回复(0)