加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++ Map快速入门

发布时间:2020-12-16 07:44:31 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #include "stdafx.h" #include iostream #include map using namespace std; int _tmain(int argc,_TCHAR* argv[]) { mapint,const char * mymap; mym

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    #include "stdafx.h"  
    #include <iostream>  
    #include <map>  
    using namespace std;  
    int _tmain(int argc,_TCHAR* argv[])  
    {  
    map<int,const char *> mymap;  
    mymap[0] = "Jack";  
    mymap[1] = "Lucy";  
    mymap.insert(pair<int,const char *>(3,"Mike"));  
    mymap.insert(pair<int,const char *>(4,"Lily"));  
    cout<<"Use iterator"<<endl;  
    map<int,const char*>::iterator it = mymap.begin();  
    while (it != mymap.end())  
    {  
    cout<<it->first<<"->"<<it->second<<endl;  
    it++;  
    }  
    cout<<"Use reverse_iterator"<<endl;  
    map<int,const char*>::reverse_iterator reit = mymap.rbegin();  
    while (reit != mymap.rend())  
    {  
    cout<<reit->first<<"->"<<reit->second<<endl;  
    reit++;  
    }  
    it = mymap.find(1);  
    if ( it != mymap.end())  
    {  
    cout<<"use find(1):"<<it->second<<endl;  
    >}  
    else  
    {  
    cout<<"Cannot find(1)"<<endl;  
    }  
      
      
    mymap.clear();  
    mymap[5] = "New";  
    mymap[6] = "Temp";  
    cout<<"Current size"<<mymap.size()<<endl;  
      
      
    }  

Member Where defined Description
key_type Associative Container map中的key类型
data_type Pair Associative Container key关联的值类型
value_type Pair Associative Container 对象类型,?pair<const key_type,data_type>,存储在map中
key_compare Sorted Associative Container Function object?通过顺序比较
value_compare Sorted Associative Container Function object?that compares two values for ordering.
pointer Container Pointer to?T.
reference Container Reference to?T
const_reference Container Const reference to?T
size_type Container An unsigned integral type.
difference_type Container A signed integral type.
iterator Container Iterator used to iterate through a?map.?[1]
const_iterator Container Const iterator used to iterate through a?map.
reverse_iterator Reversible Container Iterator used to iterate backwards through a?map.[1]
const_reverse_iterator Reversible Container Const iterator used to iterate backwards through amap.
iterator begin() Container Returns an?iterator?pointing to the beginning of the?map.
iterator end() Container Returns an?iterator?pointing to the end of themap.
const_iterator begin() const Container Returns a?const_iterator?pointing to the beginning of themap.
const_iterator end() const Container Returns a?const_iterator?pointing to the end of the?map.
reverse_iterator rbegin() Reversible Container Returns a?reverse_iterator?pointing to the beginning of the reversed map.
reverse_iterator rend() Reversible Container Returns a?reverse_iterator?pointing to the end of the reversed map.
const_reverse_iterator rbegin() const Reversible Container Returns a?const_reverse_iterator?pointing to the beginning of the reversed map.
const_reverse_iterator rend() const Reversible Container Returns a?const_reverse_iterator?pointing to the end of the reversed map.
size_type size() const Container Returns the size of the?map.
size_type max_size() const Container Returns the largest possible size of the?map.
bool empty() const Container true?if the?map's size is?.
key_compare key_comp() const Sorted Associative Container Returns the?key_compare?object used by the?map.
value_compare value_comp() const Sorted Associative Container Returns the?value_compare?object used by themap.
map() Container Creates an empty?map.
map(const key_compare& comp) Sorted Associative Container Creates an empty?map,using?comp?as thekey_compare?object.
template <class InputIterator>
map(InputIterator f,InputIterator l)
Unique Sorted Associative Container Creates a map with a copy of a range.
template <class InputIterator>
map(InputIterator f,InputIterator l,const key_compare& comp)
Unique Sorted Associative Container Creates a map with a copy of a range,using?compas thekey_compare?object.
map(const map&) Container The copy constructor.
map& operator=(const map&) Container The assignment operator
void swap(map&) Container Swaps the contents of two maps.
pair<iterator,bool>
insert(const value_type& x)
Unique Associative Container Inserts?x?into the?map.
iterator insert(iterator pos,const value_type& x)
Unique Sorted Associative Container Inserts?x?into the?map,using?pos?as a hint to where it will be inserted.
template <class InputIterator>
void insert(InputIterator,InputIterator) [2] 
Unique Sorted Associative Container Inserts a range into the?map.
void erase(iterator pos) Associative Container Erases the element pointed to by?pos.
size_type erase(const key_type& k) Associative Container Erases the element whose key is?k.
void erase(iterator first,iterator last) Associative Container Erases all elements in a range.
void clear() Associative Container Erases all of the elements.
iterator find(const key_type& k) Associative Container Finds an element whose key is?k.
const_iterator find(const key_type& k) const Associative Container Finds an element whose key is?k.
size_type count(const key_type& k) Unique Associative Container Counts the number of elements whose key is?k.
iterator lower_bound(const key_type& k) Sorted Associative Container Finds the first element whose key is not less thank.
const_iterator lower_bound(const key_type& k) const Sorted Associative Container Finds the first element whose key is not less thank.
iterator upper_bound(const key_type& k) Sorted Associative Container Finds the first element whose key greater than?k.
const_iterator upper_bound(const key_type& k) const Sorted Associative Container Finds the first element whose key greater than?k.
pair<iterator,iterator> 
equal_range(const key_type& k)
Sorted Associative Container Finds a range containing all elements whose key is?k.
pair<const_iterator,const_iterator> 
equal_range(const key_type& k) const
Sorted Associative Container Finds a range containing all elements whose key is?k.
data_type& 
operator[](const key_type& k) [3] 
map See below.
bool operator==(const map&,const map&)
Forward Container Tests two maps for equality. This is a global function,not a member function.
bool operator<(const map&,const map&)
Forward Container Lexicographical comparison. This is a global function,not a member function.

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读