C++学习笔记(三)~map三种插入数据的方法

map三种插入数据的方法

方法一:insert函数插入pair

示例

 t.insert(pair<int, string>(0, "one"));

测试代码

#include <iostream>
// 使用map 需要引入#include <map>
#include <map>
using namespace std;

int main()
{
    map<int, string> t;
    t.insert(pair<int, string>(0, "one"));
    t.insert(pair<int, string>(1, "two"));
    t.insert(pair<int, string>(2, "three"));
    map<int, string>::iterator iter;//定义迭代器
    iter = t.begin();
    // 依次输出map中的key value 使用迭代器
    while (iter != t.end())
    {
        cout << "the key is " << iter->first << " the value is " << iter->second << endl;
        iter++;
    }
    return 0;
}

运行结果
在这里插入图片描述

方法二:insert函数插入value_type数据

示例

 t.insert(map<int, string>::value_type(0, "one"));

测试代码

#include <iostream>
// 使用map 需要引入#include <map>
#include <map>
using namespace std;

int main()
{
    map<int, string> t;
    t.insert(map<int, string>::value_type(0, "one"));
    t.insert(map<int, string>::value_type(1, "two"));
    t.insert(map<int, string>::value_type(2, "three"));
    map<int, string>::iterator iter; //定义迭代器
    iter = t.begin();
    // 依次输出map中的key value 使用迭代器
    while (iter != t.end())
    {
        cout << "the key is " << iter->first << " | the value is " << iter->second << endl;
        iter++;
    }
    return 0;
}

运行结果
在这里插入图片描述

方法三:array方式插入

示例

 t[0]="one";

测试代码

#include <iostream>
// 使用map 需要引入#include <map>
#include <map>
using namespace std;

int main()
{
    map<int, string> t;
    t[0]="one";
    t[1]="two";
    t[2]="three";
    t[3]="four";
    map<int, string>::iterator iter; //定义迭代器
    iter = t.begin();
    // 依次输出map中的key value 使用迭代器
    while (iter != t.end())
    {
        cout << "the key is " << iter->first << " | the value is " << iter->second << endl;
        iter++;
    }
    return 0;
}

运行结果
在这里插入图片描述

区别

        方法一和方法二,在插入insert时,会检查当前插入数据的key值,如果与原map中已有的key值重复,则插入失败;方法三即使key值重复,依然可以插入,key、value值则是覆盖原来的值。

测试方法一,插入相同key值

#include <iostream>
// 使用map 需要引入#include <map>
#include <map>
using namespace std;

int main()
{
    map<int, string> t;
    t.insert(pair<int, string>(0, "one"));
    t.insert(pair<int, string>(1, "twco"));
    t.insert(pair<int, string>(1, "threxe"));
    map<int, string>::iterator iter;//定义迭代器
    iter = t.begin();
    // 依次输出map中的key value 使用迭代器
    while (iter != t.end())
    {
        cout << "the key is " << iter->first << " the value is " << iter->second << endl;
        iter++;
    }
    return 0;
}

结果
在这里插入图片描述
结论:t[1]="two"后,再插入key值一样的t[1]=“three”,后面的语句会失效

测试方法二,插入相同key值

#include <iostream>
// 使用map 需要引入#include <map>
#include <map>
using namespace std;

int main()
{
    map<int, string> t;
    t.insert(map<int, string>::value_type(0, "one"));
    t.insert(map<int, string>::value_type(1, "two"));
    t.insert(map<int, string>::value_type(1, "three"));
    map<int, string>::iterator iter; //定义迭代器
    iter = t.begin();
    // 依次输出map中的key value 使用迭代器
    while (iter != t.end())
    {
        cout << "the key is " << iter->first << " | the value is " << iter->second << endl;
        iter++;
    }
    return 0;
}

结果
在这里插入图片描述
结论:t[1]="two"后,再插入key值一样的t[1]=“three”,后面的语句会失效

测试方法三,插入相同key值

#include <iostream>
// 使用map 需要引入#include <map>
#include <map>
using namespace std;

int main()
{
    map<int, string> t;
    t[0]="one";
    t[1]="two";
    t[2]="three";
    t[2]="four";

    map<int, string>::iterator iter; //定义迭代器
    iter = t.begin();
    // 依次输出map中的key value 使用迭代器
    while (iter != t.end())
    {
        cout << "the key is " << iter->first << " | the value is " << iter->second << endl;
        iter++;
    }
    return 0;
}

结果
在这里插入图片描述
结论:开始t[2]=“three”,后面又t[2]=“four”,而结果是four,证明后面语句覆盖了原来的语句

注:运行环境为vscode

参考资料

https://www.cnblogs.com/maluning/p/8570717.html
https://blog.csdn.net/sevenjoin/article/details/81943864

猜你喜欢

转载自blog.csdn.net/weixin_44225182/article/details/107629818