#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <map>
#include <tuple>
// Template class to demonstrate various C++ features
template <typename T>
class ComplexClass {
public:
T data;
// Constructor
ComplexClass(T value) : data(value) {}
// Overload + operator for adding two ComplexClass objects
ComplexClass operator+(const ComplexClass& other) const {
return ComplexClass(data + other.data);
}
// Method to return the value
T getData() const {
return data;
}
};
// Function to demonstrate tuple, lambda, and map usage
auto processAndDisplay = [](const std::vector<std::string>& names) -> std::tuple<int, std::map<char, int>> {
int totalLength = 0;
std::map<char, int> charCount;
// Process names and count the occurrence of first characters
for (const auto& name : names) {
totalLength += name.length();
char firstChar = name[0];
charCount[firstChar]++;
}
return std::make_tuple(totalLength, charCount);
};
int main() {
// Create and initialize a vector of names
std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David", "Alice"};
// Call the lambda function and get the tuple result
auto [totalLength, charCount] = processAndDisplay(names);
// Output the total length of all names
std::cout << "Total length of names: " << totalLength << std::endl;
// Output the count of each first character
std::cout << "First character counts:" << std::endl;
for (const auto& entry : charCount) {
std::cout << entry.first << ": " << entry.second << std::endl;
}
// Demonstrate usage of ComplexClass template
ComplexClass<int> obj1(10);
ComplexClass<int> obj2(20);
ComplexClass<int> obj3 = obj1 + obj2; // Add two ComplexClass objects
// Output the result of addition
std::cout << "Result of ComplexClass addition: " << obj3.getData() << std::endl;
return 0;
}
ไม่มีอะไรครับ
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <map>
#include <tuple>
// Template class to demonstrate various C++ features
template <typename T>
class ComplexClass {
public:
T data;
// Constructor
ComplexClass(T value) : data(value) {}
// Overload + operator for adding two ComplexClass objects
ComplexClass operator+(const ComplexClass& other) const {
return ComplexClass(data + other.data);
}
// Method to return the value
T getData() const {
return data;
}
};
// Function to demonstrate tuple, lambda, and map usage
auto processAndDisplay = [](const std::vector<std::string>& names) -> std::tuple<int, std::map<char, int>> {
int totalLength = 0;
std::map<char, int> charCount;
// Process names and count the occurrence of first characters
for (const auto& name : names) {
totalLength += name.length();
char firstChar = name[0];
charCount[firstChar]++;
}
return std::make_tuple(totalLength, charCount);
};
int main() {
// Create and initialize a vector of names
std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David", "Alice"};
// Call the lambda function and get the tuple result
auto [totalLength, charCount] = processAndDisplay(names);
// Output the total length of all names
std::cout << "Total length of names: " << totalLength << std::endl;
// Output the count of each first character
std::cout << "First character counts:" << std::endl;
for (const auto& entry : charCount) {
std::cout << entry.first << ": " << entry.second << std::endl;
}
// Demonstrate usage of ComplexClass template
ComplexClass<int> obj1(10);
ComplexClass<int> obj2(20);
ComplexClass<int> obj3 = obj1 + obj2; // Add two ComplexClass objects
// Output the result of addition
std::cout << "Result of ComplexClass addition: " << obj3.getData() << std::endl;
return 0;
}