C++ 将不同的数据类型组合到一个void类型中
我正在创建一个搜索树来处理连接的键。ie密钥是多种数据类型的组合。例如,key可能是student\u id、student\u name、student\u age的串联…当我传递这三个值以创建key函数时,我应该如何创建这样的key?还提供了两个键如何比较它们?比较功能的实现留给读者作为练习C++ 将不同的数据类型组合到一个void类型中,c++,C++,我正在创建一个搜索树来处理连接的键。ie密钥是多种数据类型的组合。例如,key可能是student\u id、student\u name、student\u age的串联…当我传递这三个值以创建key函数时,我应该如何创建这样的key?还提供了两个键如何比较它们?比较功能的实现留给读者作为练习 #include <cstdlib> #include <string> using namespace std; class StudentKey { public:
#include <cstdlib>
#include <string>
using namespace std;
class StudentKey
{
public:
string
id_,
name_;
unsigned age_;
bool operator<(const StudentKey& rhs) const;
};
StudentKey CreateKey(const std::string& student_name, const std::string& student_id, unsigned student_age)
{
StudentKey ret;
ret.name_ = student_name;
ret.id_ = student_id;
ret.age_ = student_age;
return ret;
}
int main()
{
StudentKey key = CreateKey("name","id",42);
}
#包括
#包括
使用名称空间std;
班级学生钥匙
{
公众:
一串
同上,
姓名;;
未签名年龄;
bool operatorErrr…一个结构/类?有一些方法?请解释“into a void type”,没有任何描述和代码,这个问题毫无意义。“我如何比较两个键?”-如果它们是void类型,那么你就不能。你没有正确理解我的问题…我需要将不同的数据类型填充到一个void缓冲区中…怎么做?为什么你认为你需要一个void缓冲区?