1. Tuple Type
Tuples are used to group multiple values into a single object without defining a separate structure. They are similar to the pair template.
#include <tuple>
#include <string>
#include <vector>
#include <list>
int main() {
std::tuple<size_t, size_t, size_t> dimensions{1, 2, 3}; // Valid initialization
std::tuple<std::string, std::vector<double>, int, std::list<int>> values("constants", {3.14, 2.718}, 42, {0, 1, 2, 3, 4, 5});
auto record = std::make_tuple("ISBN-12345", 5, 25.0);
std::get<2>(record) *= 0.9; // Apply a 10% discount
using RecordType = decltype(record);
size_t fieldCount = std::tuple_size<RecordType>::value; // 3 fields
int quantity = std::get<1>(record); // Access second field
}
1.1 Using Tuples to Return Multiple Values
A common use case for tuples is returning multiple values from a function:
std::tuple<int, double> calculateStats(int a, double b) {
return std::make_tuple(a + 1, b * 2);
}
2. Bitset Type
The bitset class is used to menage sequences of bits efficiently.
#include <bitset>
#include <string>
int main() {
std::bitset<32> bits(1U); // Initialize with the lowest bit set
std::bitset<16> hexBits(0xbeef); // Hex to binary conversion
std::string binaryStr = "110011";
std::bitset<32> strBits(binaryStr); // Convert string to bitset
}
2.1 Bitset Oeprations
std::bitset<16> flags;
flags.set(3); // Set bit 3
flags.reset(3); // Clear bit 3
bool isSet = flags.test(3); // Check if bit 3 is set
3. Regular Expressions
C++ provides robust support for regular expressions through the <regex> libray.
#include <regex>
#include <string>
#include <iostream>
int main() {
std::string text = "Sample text with email@example.com";
std::regex pattern(R"((\w+)(@)(\w+)(\.)(\w+))"); // Match email addresses
std::smatch match;
if (std::regex_search(text, match, pattern)) {
std::cout << "Found: " << match.str() << std::endl;
}
}
3.1 Submatches and Replacement
std::string input = "file1.cpp file2.txt file3.cxx";
std::regex extPattern(R"((\w+)\.(cpp|cxx|cc))");
std::string replacement = "$1.extension";
std::string result = std::regex_replace(input, extPattern, replacement);
4. Random Number Generation
C++ provides better random number facilities than the old rand() function.
#include <random>
#include <vector>
int main() {
std::default_random_engine engine(std::random_device{}());
std::uniform_int_distribution<int> range(1, 100);
std::vector<int> randomNumbers(10);
for (auto& num : randomNumbers) {
num = range(engine); // Generate numbers between 1 and 100
}
}
4.1 Normal Distribution Example
std::normal_distribution<double> normal(5.0, 2.0); // Mean 5, standard deviation 2
for (int i = 0; i < 5; ++i) {
std::cout << normal(engine) << " ";
}
5. Input/Output Library Enhancements
Formatting capabilities in C++ can be controlled with manipulators and stream flags.
#include <iomanip>
std::cout << std::hex << 255 << " "; // Output in hexadecimal
std::cout << std::dec << 255 << std::endl; // Back to decimal
std::cout << std::fixed << std::setprecision(2) << 3.14159 << std::endl;
5.1 Field Width and Fill
std::cout << std::setw(10) << std::setfill('*') << 42 << std::endl;
// Output: ********42