std::pair
Create a MyPair
template class 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.
std::set
(1/2)In a building security system, door locks are opened by entering a four-digit 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, with repetitions.sorted_unique_numbers
by sorting the random_numbers
vector and removing duplicate entries.unsorted_unique_numbers
by printing unique entries from the random_numbers
in the same order they appear, without repetitions.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.
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 word followed by its frequency, separated by a colon or any other suitable delimiter.
(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
.
Vector
class that transfers ownership of the underlying data from the source vector to the destination vector. The move constructor should ensure that the source vector's data is no longer accessible after the transfer.Vector
class that allows for the efficient transfer of ownership of the underlying data from one Vector object to another. Similar to the move constructor, the move assignment operator should ensure that the source vector's data is no longer accessible after the transfer.Create a C++ program that demonstrates how std::unique_ptr
and std::shared_ptr
can be used with polymorphism.
Base
with a virtual method void display()
.Derived1
and Derived2
that override the display()
method.std::unique_ptr
and std::shared_ptr
to manage instances of Base
that actually point to Derived
objects.display()
through the smart pointers.