std::pairCreate a MyPair class template that mimics std::pair, holding two elements of any type. Implement member variables first and second, with constructors for default and custom initialization. Overload operator==, operator!=, and operator< for equality and lexicographical comparison. Test your implementation using various data types such as int, double, and std::string.
Note: Lexicographical comparison means comparing first elements first; if equal, then compare second elements.
std::set (1/2)In a building security system, door locks are opened by entering an access code into a keypad. The access code's validation process is handled through an Access object with the following interface:
class Access
{
public:
void activate(unsigned int code);
void deactivate(unsigned int code);
bool is_active(unsigned int code) const;
};
Each employee is assigned a unique access code, which can be activated using the activate() function. When an employee leaves the company, their access code can be deactivated using the deactivate() function.
std::set (2/2)Your task is to implement the Access class as described above. Write a test program that accomplishes the following tasks:
Access object.std::map (1/2)In the previous exercise, the customer using the security system wants to associate an access level with each access code. Users with higher access levels should be able to open doors to more security-sensitive areas of the building compared to users with lower access levels. Start with your solution from the previous exercise and make the following modifications to the Access class:
class Access
{
public:
void activate(unsigned int code, unsigned int level);
void deactivate(unsigned int code);
bool is_active(unsigned int code, unsigned int level) const;
};
The is_active() function should return true if the specified access code has an access level greater than or equal to the specified access level. If the access code is not active at all, it should return false.
std::map (2/2)Now, update the main program to perform the following tasks:
Access object, activate code 1234 with access level 1, code 9999 with access level 5, and code 9876 with access level 9.random_numbers that contains 100 random integers between 0 and 9.sorted_numbers by sorting the random_numbers vector in ascending order (keep duplicates).sorted_unique_numbers by sorting the random_numbers vector (remove duplicates).unsorted_unique_numbers by printing unique entries from the random_numbers in the same order they appear, without repetitions.Hints: Use std::sort() for sorting. Use std::unique() combined with erase() for removing consecutive duplicates. For task 4, consider using std::unordered_set to track seen elements.
The file input.txt contains a list of random complete sentences in English. Develop a C++ program that reads the file, calculates the frequency of each word in the text, and outputs the word-frequency pairs to a new file in a dictionary format.
Write a C++ program to process the input text file by splitting it into words and counting the occurrences of each unique word. Spaces and punctuation should be discarded. Convert words to lowercase for case-insensitive counting.
The program should generate a new file (named output.txt) containing the word-frequency pairs in a dictionary format. Each line in the output file should consist of a list of entries of the form word: frequency.
Bonus: sort the output by frequency, in descending order. If two words have the same frequency, then sort them alphabetically.
Define a class Vector that represents a one-dimensional vector of double values, stored as a raw pointer double *data, along with its size. Implement constructors, the copy constructor, the copy assignment operator, and the destructor.
Create a C++ program demonstrating smart pointers with polymorphism:
Base with a method virtual void display() const and a virtual destructor.Derived1 and Derived2 that override display().Base that actually point to Derived objects. Demonstrate:
std::unique_ptr<Base> pointing to Derived objects;std::shared_ptr<Base> pointing to Derived objects;std::unique_ptr (using std::move);std::shared_ptr (observe reference counting);display() through the smart pointers.