Set and STL operations for specific stl operations

Source: Internet
Author: User

Set and STL operations for specific stl operations
Wrong operation before sorted set. Sorry.
/* Begin (), returns the first element of the set container

End (), returns the last element of the set container.

Clear (), delete all elements in the set container

Empty () to determine whether the set container is empty

Max_size () returns the maximum number of elements that a set container may contain.

Size (), returns the number of elements in the current set container

Rbegin, returns the same value as end ()

Rend (), returns the same value as rbegin ()

# Include <iostream>
# Include <set>
Using namespace std;
Int main ()
{
Set <int> s;
S. insert (1 );
S. insert (2 );
S. insert (3 );
S. insert (1 );
Printf ("set size value: % d", s. size ());
Printf ("set maxsize value: % d", s. max_size ());
Printf ("the first element in set is: % d", * s. begin ());
Cout <"the last element in the set is:" <* s. end () <endl;
S. clear ();
If (s. empty ())
{
Cout <"set is empty !!! "<Endl;
}
Cout <"set size value:" <s. size () <endl;
Cout <"set maxsize value:" <s. max_size () <endl;
Return 0;
}
Count () is used to find the number of times a key value appears in the set. This function is not very practical in set, because a key value can only appear 0 or 1 time in set, so it becomes to judge whether a key value has appeared in set.
# Include <iostream>
# Include <set>
Using namespace std;
Int main ()
{
Set <int> s;
S. insert (1 );
S. insert (2 );
S. insert (3 );
S. insert (1 );
Printf ("the number of occurrences of 1 in set is: % d \ n", s. count (1 ));
The number of times that cout <"set 4 appears is:" <s. count (4) <endl;
Return 0;
}
When _range (), a pair of locators are returned, indicating the first element greater than or equal to the given key value and the first element greater than the given key value. The returned value is of the pair type, if the returned result of this pair of locators fails, it will be equal to the value of end.
# Include <iostream>
# Include <set>
# Include <stdio. h>
Using namespace std;
Int main ()
{
Set <int> s;
Set <int>: iterator iter;
For (int I = 1; I <= 5; ++ I)
{
S. insert (I );
}
For (iter = s. begin (); iter! = S. end (); ++ iter)
{
Cout <* iter <"";
}
Cout <endl;
Pair <set <int >:: const_iterator, set <int >:: const_iterator> pr;
Pr = s. interval _range (3 );
Cout <"the first number greater than or equal to 3 is:" <* pr. first <endl;
Cout <"the first number greater than 3 is:" <* pr. second <endl;
Return 0;
}
Erase (iterator) to delete the value pointed to by iterator
Erase (first, second), delete the value between the first and second locators
Erase (key_value), delete the key value key_value
# Include <iostream>
# Include <set>
Using namespace std;
 
Int main ()
{
Set <int> s;
Set <int >:: const_iterator iter;
Set <int>: iterator first;
Set <int>: iterator second;
For (int I = 1; I <= 10; ++ I)
{
S. insert (I );
}
// Delete the first type
S. erase (s. begin ());
// Delete the second type
First = s. begin ();
Second = s. begin ();
Second ++;
Second ++;
S. erase (first, second );
// Delete the third type
S. erase (8 );
Cout <"after deletion, the elements in the set are :";
For (iter = s. begin (); iter! = S. end (); ++ iter)
{
Cout <* iter <"";
}
Cout <endl;
Return 0;
}
Insert (key_value); insert key_value into set. The returned value is pair <set <int >:: iterator, bool>. bool indicates whether the insert is successful, iterator indicates the insertion position. If the key_value is already in set, the key_value indicated by iterator is in set.
Inset (first, second); inserts the elements between the first and second positions into the set. The returned value is void.
# Include <iostream>
# Include <set>
Using namespace std;
Int main ()
{
Int a [] = {1, 2, 3 };
Set <int> s;
Set <int>: iterator iter;
S. insert (a, a + 3 );
For (iter = s. begin (); iter! = S. end (); ++ iter)
{
Cout <* iter <"";
}
Cout <endl;
Pair <set <int >:: iterator, bool> pr;
Pr = s. insert (5 );
If (pr. second)
{
Cout <* pr. first <endl;
}
Return 0;
}
Lower_bound (key_value), returns the first locator greater than or equal to key_value
Upper_bound (key_value), returns the last locator greater than or equal to key_value
# Include <iostream>
# Include <set>
Using namespace std;
Int main ()
{
Set <int> s;
S. insert (1 );
S. insert (3 );
S. insert (4 );
Cout <* s. lower_bound (2) <endl;
Cout <* s. lower_bound (3) <endl;
Cout <* s. upper_bound (3) <endl;
Return 0;
}*/

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.