STL之MAP和Vector

MAP

map作为STL中的映射容器非常好用,我们来说一下map的遍历。

map.first为key值,map.second为value值,key不可修改,value可修改。

1. 1 遍历1

定义一个迭代指针iter,使其指向map,实现对map的遍历。

#include <iostream>                                                                                                                                                                                         
#include <map>                                                                  
#include <string>                                                               
                                                                                
using namespace std;                                                            
                                                                                
int main()                                                                      
{                                                                               
    map<string,int>M;                                                           
    M["Kaito"]=1;                                                               
    M["Aoko"]=2;                                                                
    M["Shinichi"]=3;                                                            
    M["Lan"]=4;                                                                 
    map<string,int>::iterator iter;//定义一个迭代指针iter                       
    for(iter=M.begin(); iter!=M.end(); iter++)                                  
        cout<<iter->first <<"->"<<iter->second<<endl;                           
    return 0;                                                                   
}   

1.2 遍历2

    for (const auto& [name, num] : M) {                                                                                                            
        cout<<"name="<<name<<" num="<<num<<" "<<endl;                           
    }  

2插入值

map.first为key值,map.second为value值,key不可修改,value可修改。

#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int main(){
    map<string,int> test;
    test.insert(make_pair("test1",3));//test["test1"]=1
    test.insert(make_pair("test2",2));//test["test2"]=2
    test.insert(make_pair("test1",1));//test["test1"]=1

    cout<<"The num of test1:"<<test.count("test1")<<endl;
    for (const auto& [name, num] : test) {
	cout<<"name="<<name<<" num="<<num<<" "<<endl;
    }
    
}

插入(“test1”,3)后,无法再插入(“test1”,1),因为key值是唯一的。所以count值只能是1或者0。1是存在这个字符串,“0”是不存在这个字符串。

2. Vector

2.1遍历

    std::vector<std::string> triggers(args.begin() + 1, args.end());               
    for (auto it = triggers.begin(); it != triggers.end(); it ++) {                
        std::cout<<"tom it="<<*it<<std::endl;                                                                                                                                                               
    }  
发布了120 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/chengbeng1745/article/details/104100156
今日推荐