STL associated container-set

Source: Internet
Author: User

 


The set container is very similar to map. The difference is that the set does not store key/value pairs. In set, the value itself is the key. If you want to store information that does not display keys, but want to quickly insert, search, and delete elements, set is useful.

Set provides almost the same interface as map. The main difference is that set does not provide operator []. In addition, although not explicitly stated in the standard, most implementation commands make set iterator equivalent to const_iteraotr, so the set element cannot be modified through iterator. Even if your STL version allows you to use an iterator to modify the set element, you should avoid this because modifying the elements in the set (still in the container) will disrupt the ordered order.

Set example: Access Control List

One way to achieve basic security in a computer system is through the access control list. Each entity (such as a file or device) on the system has a list of users that are allowed to access the entity. Generally, users with special permissions can only add and delete users from the license list of an entity. Internally, the set container provides a good way to represent the access control list. Each object can have a set, including all user names that are allowed to access the object. The following is a simple class definition of the access control list.


[Cpp]
# Include <set>
# Include <string>
# Include <list>
Using std: set;
Using std: string;
Using std: list;
 
 
Class AccessList
{
Public:
AccessList (){}

// Adds the user to the permission list
Void addUser (const string & user );

// Remove the user from the permission list
Void removeUser (const string & user );

// Returns true if user is in the permission list
Bool isAllowed (const string & user) const;

// Returns a list of all the users who have permissions
List <string> getAllUsers () const;

Protected:
Set <string> mAllowed;
}

# Include <set>
# Include <string>
# Include <list>
Using std: set;
Using std: string;
Using std: list;


Class AccessList
{
Public:
AccessList (){}
 
// Adds the user to the permission list
Void addUser (const string & user );
 
// Remove the user from the permission list
Void removeUser (const string & user );
 
// Returns true if user is in the permission list
Bool isAllowed (const string & user) const;
 
// Returns a list of all the users who have permissions
List <string> getAllUsers () const;
 
Protected:
Set <string> mAllowed;
}
The following is a method definition:


[Cpp]
# Include "AccessList. h"
Using namespace std;
 
Void AccessList: addUser (const string & user)
{
MAllowed. insert (user );
}
 
Void AccessList: removeUser (const string & user)
{
MAllowed. erase (user );
}
 
Bool AccessList: isAllowed (const string & user) const
{
Return (mAllowed. count (user) = 1 );
}
 
List <string> AccessList: getAllUsers () const
{
List <string> users;
Users. insert (users. end (), mAllowed. begin (), mAllowed. end ());
Return (users );
}

# Include "AccessList. h"
Using namespace std;

Void AccessList: addUser (const string & user)
{
MAllowed. insert (user );
}

Void AccessList: removeUser (const string & user)
{
MAllowed. erase (user );
}

Bool AccessList: isAllowed (const string & user) const
{
Return (mAllowed. count (user) = 1 );
}

List <string> AccessList: getAllUsers () const
{
List <string> users;
Users. insert (users. end (), mAllowed. begin (), mAllowed. end ());
Return (users );
}
The following is a simple test procedure:


[Cpp]
# Include "AccessList. h"
# Include <iostream>
# Include <iterator>
 
Using namespace std;
 
Int main ()
{
AccessList fileX;
FileX. addUser ("nsolter ");
FileX. addUser ("klep ");
FileX. addUser ("baduser ");
FileX. removeUser ("baduser ");
 
If (fileX. isAllowed ("nsolter "))
{
Cout <"nsolter has permission! "<Endl;
}
If (fileX. isAllowed ("baduser "))
{
Cout <"baduser has permission! "<Endl;
}
 
List <string> users = fileX. getAllUsers ();
For (list <string >:: const_iterator it = users. begin ();
It! = Users. end (); ++ it)
{
Cout <* it <"";
}
Cout <endl;
 
Return 0;
}

# Include "AccessList. h"
# Include <iostream>
# Include <iterator>

Using namespace std;

Int main ()
{
AccessList fileX;
FileX. addUser ("nsolter ");
FileX. addUser ("klep ");
FileX. addUser ("baduser ");
FileX. removeUser ("baduser ");

If (fileX. isAllowed ("nsolter "))
{
Cout <"nsolter has permission! "<Endl;
}
If (fileX. isAllowed ("baduser "))
{
Cout <"baduser has permission! "<Endl;
}

List <string> users = fileX. getAllUsers ();
For (list <string >:: const_iterator it = users. begin ();
It! = Users. end (); ++ it)
{
Cout <* it <"";
}
Cout <endl;

Return 0;
}

 

For the above Code, the error "error C2143: syntax error: missing '; 'before' namespace'" is always reported during compilation in VC 6.0 ,,,, after checking for N times, I cannot find out why this error is prompted !!

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.