Simple application of set container and map container

set container some functions, from Baidu other big guy has summed up, if there is infringement, please contact delete!

The member functions of set are listed as follows:

c++ stl container set member function: begin() -- returns the iterator pointing to the first element

c++ stl container set member function: clear() -- clear all elements

c++ stl container set member function: count() -- returns the number of value elements

Set member function of c++ stl container: empty() -- returns true if the set is empty

set member function of c++ stl container: end() -- return iterator pointing to the last element

Set member function of c++ stl container: equal? Range() -- two iterators that return the upper and lower limits of the set equal to the given value

Set member function of c++ stl container: erase() -- delete elements in the set

c++ stl container set member function: find() -- returns an iterator that points to the found element

Set member function of c++ stl container: get allocator() -- returns the allocator of the set

Set member function of c++ stl container: insert() -- insert elements into the set

c++ stl container set member function: lower_bound() -- returns the iterator pointing to the first element greater than (or equal to) a value

set member function of c++ stl container: key_comp() -- return a function for value comparison between elements

Set member function of c++ stl container: max_size() -- returns the maximum limit of the elements that the set can hold

Set member function of c++ stl container: rbegin() -- return the reverse iterator pointing to the last element in the set

Set member function of c++ stl container: rend() -- returns the reverse iterator pointing to the first element in the set

Set member function of c++ stl container: size() -- number of elements in the set

Set member function of c++ stl container: swap() -- exchange two set variables

c++ stl container set member function: upper_bound() -- returns an iterator greater than a value element

set member function of c++ stl container: value_comp() -- returns a function used to compare values between elements

 1 #include<iostream>
 2 #include<set>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     set<int>s;//Define shaping set Container variable s 
 8     //Insert operation 
 9     s.insert(1);//insert Insertion function 
10     s.insert(3);
11     s.insert(5);
12     set<int>::iterator ite;//statement set Container pointer variable ite 
13     //Lookup operation 
14     ite = s.find(1);//find Lookup function 
15     if(ite == s.end())//Judge the pointer variable to see if the pointer points to the last element of the container 
16         cout << "no found" << endl;
17     else
18         cout << "found" << endl;
19     
20     ite = s.find(2);
21     if(ite == s.end())
22         cout << "no found" << endl;
23     else
24         cout << "found" << endl;
25     //Delete operation 
26     s.erase(3);//container set Delete the specified element operation, erase Delete function 
27     //Find the deleted elements in turn 
28     if(s.count(3) != 0)
29         cout << "found" << endl;
30     else
31         cout << "no found" << endl;
32     //Traversal container operation 
33     for(ite = s.begin();ite != s.end();ite++)//begin The function points to the first element of the container and performs a for loop 
34         cout << *ite << "P" << endl;//Ergodic output 
35     return 0;
36 }

Operation result:

 

 

Here is a simple application of map container

begin() returns the iterator pointing to the map header
clear() delete all elements
count() returns the number of occurrences of the specified element
empty() returns true if map is empty
end() returns the iterator pointing to the end of the map
erase() removes an element
find() finds an element
insert() insert element
Key? Comp() returns the function of the comparison element key
lower_bound() returns the key value > = the first position of the given element
max_size() returns the maximum number of elements that can be accommodated
rbegin() returns a reverse iterator pointing to the tail of the map
rend() returns a reverse iterator pointing to the map header
size() returns the number of elements in the map
swap() exchanges two map s
upper_bound() returns the first position of the key value > given element
Value ﹐ comp() returns the function of comparing element value

The simple application of the following map container uses the make ﹣ pair function, i.e. the function when two values need to be returned. For details, please go online to find out. There is not too much detail here.

#include<iostream>
#include<map>
using namespace std;

int main()
{
    //statement(int For keys, const char*Value) 
    map<int,const char*>m;
    //Insert element, using insert function 
    m.insert(make_pair(1,"ONE"));
    m.insert(make_pair(10,"TEN"));
    m[100] = "HUNDRED";
    //Search element 
    map<int,const char*>::iterator ite;
    ite = m.find(1);
    cout << ite->second << endl;
    
    ite = m.find(2);
    if(ite == m.end())
        cout << "no found" << endl;
    else    
        cout << ite->second << endl;
    
    cout << m[10] << endl;
    //Delete elements 
    m.erase(10);
    //Go through all elements and output 
    for(ite = m.begin();ite != m.end();ite++)
        cout << ite->first << ":" << ite->second << endl;
    return 0;
}

Operation result:

Tags: C++

Posted on Sun, 03 Nov 2019 18:49:07 -0800 by JonathanS