Home >Backend Development >C#.Net Tutorial >Take you to learn more about map (key-value pairs) and set (collection) in C++

Take you to learn more about map (key-value pairs) and set (collection) in C++

烟雨青岚
烟雨青岚forward
2020-07-06 13:48:513808browse

Take you to learn more about map (key-value pairs) and set (collection) in C++

C Learning: map set (key-value pair)

Set (set) and map (map) belong to Non-linear structure container class

The internal implementation is a balanced binary tree

map is a correlation container of STL, which provides one-to-one data processing capabilities

The first one can be called a keyword, and each keyword Can only appear once in the map
The second is called the value of the keyword

set set

begin Syntax:

iterator begin();
Returns an iterator pointing to the first element in the current collection.

clear Syntax:

void clear();
Clear all elements in the current collection.

count Syntax:

size_type count( const key_type &key );
Returns the number of elements with a certain value that appear in the current collection.

empty Syntax:

bool empty();
If the current collection is empty, return true; otherwise, return false.

end Syntax:

const_iterator end();
Returns an iterator pointing to the last element in the current collection.

equal_range Syntax:

pair equal_range( const key_type &key );
Returns two iterators with upper and lower bounds in the collection that are equal to the given value.

erase Syntax:

void erase( iterator i ); 
void erase( iterator start, iterator end ); 
size_type erase( const key_type &key );

Description:
● Delete the i element;
● Delete the elements starting from start to end; ● Delete all elements equal to the key value (return the number of deleted elements).

find syntax:

iterator find( const key_type &key );
Find an element equal to the key value in the current collection and return an iterator pointing to the element; if not found, return a pointer to the end of the collection An iterator of elements.

get_allocator Syntax:

allocator_type get_allocator();
Returns the allocator of the current collection.

insert Syntax:

iterator insert( iterator i, const TYPE &val ); 
void insert( input_iterator start, input_iterator end ); 
pair insert( const TYPE &val );

Description:
● Insert val before iterator i;
● Return the iterator from start to end. The element is inserted into the collection;
● Insert the val element into the current collection, and return an iterator pointing to the element and a Boolean value to indicate whether val was successfully inserted. (It should be noted that two identical elements cannot be inserted into sets (Sets).)

lower_bound Syntax:

iterator lower_bound( const key_type &key );
Returns a pointer that is greater than or An iterator equal to the first element of the key value.

key_comp Syntax:

key_compare key_comp();
Returns a function object used for value comparison between elements.

max_size Syntax:

size_type max_size();
Returns the maximum limit of the elements that the current collection can accommodate.

rbegin Syntax:

reverse_iterator rbegin();
Returns a reverse iterator pointing to the last element in the current collection.

rend Syntax:

reverse_iterator rend();
Returns a reverse iterator pointing to the first element in the collection.

size Syntax:

size_type size();
Returns the number of elements in the current collection.

swap Syntax:

void swap( set &object );
Exchange elements in the current collection and the object collection.

upper_bound Syntax:

iterator upper_bound( const key_type &key );
Returns an iterator pointing to elements greater than the Key value in the current collection.

value_comp Syntax:

value_compare value_comp();
Returns a function object used to compare values ​​between elements

Simple example code:

/*
 * ===========================================================================
 *
 *       Filename:  setTest.cpp
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年07月01日 22时20分58秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */
#include<iostream>
#include<set>
using namespace::std;


void showSet(const set<int> i_set){
  //通过迭代器的形式输出set集合
  cout<<"===========the show start==========="<<endl;
  set<int>::const_iterator c_iter;
  for(c_iter = i_set.begin(); c_iter !=i_set.end();c_iter++){
    cout<< "i_set:"<<*c_iter << endl;
  }
  cout<<"===========the show end============="<<endl;
}

void showReverseSet(set<int> r_set){
  cout<<"==========the show reverse set start============="<<endl;
  //通过反向迭代器输出
  set<int>::reverse_iterator r_iter;
  for(r_iter = r_set.rbegin();r_iter!=r_set.rend();r_iter++){
    cout<< "r_set:"<< *r_iter <<endl;
  }
  cout<<"==========the show reverse set end==============="<<endl;
}


int main(int argc,char *argv[]){
  //set的常用几种构造函数
  int array[6] = {11,3,25,7,9,6};
  //第一种构造函数
  set<int> first_set;
  //第二种构造函数
  set<int> second_set(array,array+6);
  showSet(second_set);
  //第三种构造函数
  set<int> third_set(second_set.begin(),second_set.end());
  showSet(third_set);

  //set的插入insert函数
  first_set.insert(3);
  first_set.insert(6);
  first_set.insert(5);
  first_set.insert(5);
  showSet(first_set);

  //返回当前集合中出现某个值的数量count
  int count = first_set.count(5);
  cout<< "the count:"<< count << endl;

  //返回当前集合是否是null的  empty函数
  bool setIsEmpty = first_set.empty();
  cout << "the set empty type:"<<setIsEmpty <<endl;

  //删除某个元素  erase
  first_set.erase(3);
  showSet(first_set);

  //set的大小
  cout<<"the set size:"<< first_set.size() << endl;
  
  showReverseSet(first_set);

  //交换集合中的元素
  first_set.swap(second_set);
  showSet(first_set);
  showSet(second_set);
  
  //通过find函数来寻找集合中的元素
  set<int>::iterator iter;
  iter = first_set.find(3);
  first_set.erase(iter);
  showSet(first_set);

  first_set.clear();
  return 0;
}


Use of map key-value pairs
C MapsC Maps are used to store "keyword/value" pairs

begin Syntax:

  iterator begin();

begin() function returns an iterator pointing to the first element of the map.

clear Syntax:

void clear();

clear() function deletes all elements in the map.

count Syntax:

  size_type count( const KEY_TYPE &key );

The count() function returns the number of elements in the map whose key value is equal to key.

empty Syntax:

  bool empty();

empty() function returns true (true) if the map is empty, otherwise returns false (false).

end Syntax:

  iterator end();

end() function returns an iterator pointing to the end of the map.

equal_range Syntax:

  pair equal_range( const KEY_TYPE &key );

equal_range() function returns two iterators - one pointing to the first element with the key value key, and the other pointing to the last element The element whose key value is key.

erase syntax:

  void erase( iterator pos );
  void erase( iterator start, iterator end );
  size_type erase( const KEY_TYPE &key );

erase() function deletes the element at pos position, or deletes the element between start and end, or deletes those whose value is key of all elements.

find Syntax:

  iterator find( const KEY_TYPE &key );

The find() function returns an iterator pointing to the element with the key value key. If it is not found, it returns an iterator pointing to the end of the map.

get_allocator Syntax:

  allocator_type get_allocator();

The get_allocator() function returns the map's configurator.

insert Syntax:

  iterator insert( iterator pos, const pair<KEY_TYPE,VALUE_TYPE> &val );
  void insert( input_iterator start, input_iterator end );
  pair<iterator, bool> insert( const pair<KEY_TYPE,VALUE_TYPE> &val );

insert() function:

Insert val after pos, and then return a pointer to this element iterator.

Insert elements from start to end into the map.

Insert val only if val does not exist. The return value is an iterator pointing to the inserted element and a bool value describing whether it was inserted.

key_comp Syntax:

  key_compare key_comp();

key_comp() function returns a function that compares keys.

lower_bound Syntax:######
  iterator lower_bound( const KEY_TYPE &key );

lower_bound()函数返回一个迭代器,指向map中键值>=key的第一个元素。

max_size 语法:

  size_type max_size();

max_size()函数返回map能够保存的最大元素个数。

rbegin 语法:

  reverse_iterator rbegin();

rbegin()函数返回一个指向map尾部的逆向迭代器。

rend 语法:

  reverse_iterator rend();

rend()函数返回一个指向map头部的逆向迭代器。

size 语法:

  size_type size();

size()函数返回map中保存的元素个数。

swap 语法:

  void swap( map &obj );

swap()交换obj和现map中的元素。

upper_bound 语法:

  iterator upper_bound( const KEY_TYPE &key );

upper_bound()函数返回一个迭代器,指向map中键值>key的第一个元素。

value_comp 语法:

  value_compare value_comp();

value_comp()函数返回一个比较元素value的函数。

map集合的一些简单的使用

#include<iostream>
#include<map>
#include<string>
using namespace::std;
/* *
 *map集合的简单使用
 *
 * */



class Compare{
  public:
    bool operator()(int a,int b){
      return a > b;
    }

};

int main(int argc,char* argv[]){
  //定义一个map的对象
  map<int,string> num_convert_map;
  map<int,string>::iterator m_iter;

  //采用pair来进行键值对的添加
  num_convert_map.insert(pair<int,string>(1,"one"));
  //通过map集合内部的value_type来进行插入键值对
  num_convert_map.insert(map<int,string>::value_type(2,"two"));
  //通过make_pair来进行键值对的插入
  num_convert_map.insert(make_pair(3,"three"));

  //map重载了[],[键] = "值"
  num_convert_map[0] = "zero";


  //输出map中的元素
  for(m_iter =num_convert_map.begin();m_iter != num_convert_map.end() ;m_iter++){
    //第一个元素iter->first  第二个元素 iter->second
    cout << m_iter->first <<":"<< m_iter -> second << endl;
  }

  //通过键来查询值
  cout<<num_convert_map[3]<<endl;
  
  map<string,string> string_map;
  string_map.insert(pair<string,string>("张三","初中"));
  string_map["李四"] = "高中";
  //由此重写后的map对象的[]运算符是支持泛型的
  cout << string_map["张三"]<<endl;
  

  //输出map的大小
  cout<< "map size:" << num_convert_map.size() <<endl;
  
  //通过Compare将map排序,通过赋值拷贝的构造函数
  map<int,string,Compare> num_convert_map2(num_convert_map.begin(),num_convert_map.end());
  num_convert_map2.insert(pair<int,string>(5,"张三"));  
  num_convert_map2.insert(pair<int,string>(3,"李四"));  
  num_convert_map2.insert(pair<int,string>(1,"赵柳"));  
  cout<< "-------------"<<endl;
  for(m_iter = num_convert_map2.begin();m_iter != num_convert_map2.end();m_iter++){
    cout<<m_iter->first <<";"<<m_iter->second<<endl;
  }

  return 0;
}

感谢大家的阅读,希望大家收益多多。

本文转自:  https://blog.csdn.net/qq_29924041/article/details/74080102  

推荐教程:《C语言》                               

The above is the detailed content of Take you to learn more about map (key-value pairs) and set (collection) in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete