Online Book Reader

Home Category

Professional C__ - Marc Gregoire [214]

By Root 1472 0

Ron Weasley

Hermione Granger

Hagrid

set

The set container, defined in the header file, is very similar to the map. The difference is that instead of storing key/value pairs, in sets the value itself is the key. The set containers are useful for storing information in which there is no explicit key, but which you want to have in sorted order with quick insertion, lookup, and deletion.

The interface supplied by set is almost identical to that of the map. The main difference is that the set doesn’t provide operator[].

Note that a set also supports the C++11 uniform initialization.

Although the standard doesn’t state it explicitly, most implementations make the iterator of a set identical to const_iterator, such that you can’t modify the elements of the set through the iterator. Even if your version of the STL permits you to modify set elements through an iterator, you should avoid doing so because modifying elements of the set while they are in the container would destroy the order.

set Example: Access Control List

One way to implement basic security on a computer system is through access control lists. Each entity on the system, such as a file or a device, has a list of users with permissions to access that entity. Users can generally be added to and removed from the permissions list for an entity only by users with special privileges. Internally, the set container provides a nice way to represent the access control list. You could use one set for each entity, containing all the usernames who are allowed to access the entity. Here is a class definition for a simple access control list:

using std::set;

using std::string;

using std::list;

using std::initializer_list;

class AccessList

{

public:

// Default constructor

AccessList() {}

// Constructor to support C++11 uniform initialization.

AccessList(const initializer_list& initlst);

// Adds the user to the permissions list.

void addUser(const string& user);

// Removes the user from the permissions list.

void removeUser(const string& user);

// Returns true if the user is in the permissionns list.

bool isAllowed(const string& user) const;

// Returns a list of all the users who have permissions.

list getAllUsers() const;

protected:

set mAllowed;

};

Code snippet from AccessControlList\AccessList.h

Here are the method definitions:

AccessList::AccessList(const initializer_list& initlst)

{

for (auto& user : initlst) {

addUser(user);

}

}

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 AccessList::getAllUsers() const

{

list users;

users.insert(users.end(), mAllowed.begin(), mAllowed.end());

return users;

}

Code snippet from AccessControlList\AccessList.cpp

Finally, here is a simple test program:

AccessList fileX = {"nsolter", "klep", "baduser"};

fileX.removeUser("baduser");

if (fileX.isAllowed("nsolter")) {

cout << "nsolter has permissions" << endl;

}

if (fileX.isAllowed("baduser")) {

cout << "baduser has permissions" << endl;

}

auto users = fileX.getAllUsers();

for (auto& user : users) {

cout << user << " ";

}

Code snippet from AccessControlList\AccessListTest.cpp

The output of this program is as follows:

nsolter has permissions

klep nsolter

The preceding example uses a few C++11 features. One of the constructors for the AccessList class uses an initializer_list as parameter so that you can use the C++11 uniform initialization syntax, as demonstrated in the test program for initializing the variable fileX. The example also uses the C++11 range-based for loop and the auto keyword.

multiset

The multiset is to the set what the multimap is to the map. The multiset supports all the operations of the set, but it allows multiple elements that are equal to each other to be stored in the container simultaneously. We don’t show an example of the multiset because it

Return Main Page Previous Page Next Page

®Online Book Reader