飞道的博客

C++ 学习笔记6--set、map(key-value)结构

217人阅读  评论(0)

目录:

  1. set初始化、遍历、查找、插入、下标
    1.1 set的初始化
    1.2 set的遍历
    1.3 set的查找
    1.4 set的插入
    1.5 set的下标访问
    1.6 set的修改
  2. map初始化、遍历、查找、插入、下标
    2.1 map的初始化
    2.2 map的遍历
    2.3 map的查找
    2.4 map的插入
    2.5 map的下标访问

1. set初始化、遍历、查找、插入、下标

set的特点:

1、关键字必须唯一,不能重复
2、默认情况下,set中的key会按照升序进行排序
3、set的底层实现是红黑树

1.1 set的初始化

set number = {1, 3, 5, 8, 9, 3, 4, 3, 5};//1、使用大括号初始化

int arr[10] = {1, 8, 9, 5, 3, 7, 2, 3};
set number(arr, arr + 10);//2、使用迭代器范围的形式

1.2 set的遍历

迭代器可以看成是一个高级指针
set::iterator it;//声明迭代器
for(it = number.begin(); it != number.end(); ++it)
{
cout << it << " ";
}
cout << endl;

/
auto a = 10; */ //当有错误时加上这条
for(auto &elem : number)
{
cout << elem << " ";
}
cout << endl;

1.3 set的查找

size_t cnt1 = number.count(9);
size_t cnt2 = number.count(10);
cout << "cnt1 = " << cnt1 << endl;
cout << "cnt2 = " << cnt2 << endl;

/* auto it2 = number.find(10); */
auto it2 = number.find(9); //在set中找值为9的数
if(it2 == number.end()) //若找到默认都没找到
{
cout << “该元素不存在set中” << endl;
}
else //若找到了
{
cout << *it2 << endl;
}
cout << endl;

1.4 set的插入

pair<set::iterator, bool> ret = number.insert(7); //插入7
if(ret.second) //上面ret的那一行的bool也就是ret.second
{
cout << “插入成功” << endl;
}
else
{
cout << “插入失败” << endl;
}
//遍历
for(auto &elem : number)
{
cout << elem << " ";
}
cout << endl;

1.5 set的下标访问

下标访问,set不支持下标访问
cout << endl;
/* cout << number[0] << endl;//error */

1.6 set的修改

set不能进行修改,为了保证红黑树结构的稳定
cout <<endl << “修改” << endl;
it = number.begin();
cout << it << endl;
/
*it = 100;//error */

2. map初始化、遍历、查找、插入、下标

map特点:

map特点
1、key值必须唯一,不存在相同的key,但是value值可以相同,也可以不同
2、默认情况下,按照key值进行升序排列
3、set的底层实现是红黑树

(尝试下两种风格的md)
2.1 map的初始化

    map<string, string> number = {
   
        pair<string, string>("123", "wuhan"),
        pair<string, string>("hello", "shanghai"),
        pair<string, string>("123", "wuhan"),
        {
   "345", "nanjing"},
        {
   "0755", "shenzhen"},
        {
   "8888", "shenzhen"}
    };

2.2 map的遍历

    for(auto &elem : number)
    {
   
        cout << elem.first << " " << elem.second << endl;
    }
    

2.3 map的查找

    cout <<endl;
    size_t cnt1 = number.count("123");
    size_t cnt2 = number.count("wuhan");
    cout << "cnt1 = " << cnt1 << endl;
    cout << "cnt2 = " << cnt2 << endl;
//或
   auto it2 = number.find("123");
    if(it2 == number.end())
    {
   
        cout << "该元素不存在map中" << endl;
    }
    else
    {
   
        cout << it2->first << " " << it2->second << endl;
    }
    cout << endl;

2.4 map的插入

    //插入
    /* pair<map<string, string>::iterator, bool> ret = */
    /*     number.insert(pair<string, string>("190", "xinjiang")); */
    auto ret = number.insert({
   "190", "xinjiang"});
    if(ret.second)
    {
   
        cout << "插入成功" << ret.first->first 
            << "  " << ret.first->second << endl;
    }
    else
    {
   
        cout << "插入失败" << endl;
    }
    for(auto &elem : number)
    {
   
        cout << elem.first << " " << elem.second << endl;
    }

2.5 map的下标访问

   cout << endl;
    cout << "number[\"123\"] = " << number["123"] << endl;
    //查询key为9999的值,但是没value
    cout << "number[\"9999\"] = " << number["9999"] << endl;
    for(auto &elem : number)
    {
   
        cout << elem.first << " " << elem.second << endl;
    }

    cout << endl << endl;
    //插入key为9999,value为beijing的值
    number["9999"] = "beijing";
    for(auto &elem : number)
    {
   
        cout << elem.first << " " << elem.second << endl;
    }

    cout << endl;
    //修改map中key为123的value为beijing
    /* number["123"] = "beijing"; */这句相当于下一句
    number.operator[]("123").operator=("beijing");
    for(auto &elem : number)
    {
   
        cout << elem.first << " " << elem.second << endl;
    }
    

转载:https://blog.csdn.net/weixin_43679037/article/details/117043611
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场