由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教高手一个C++问题
相关主题
请问关于c++实现singleton的问题?how to create an instance of base class in its derived class definition
C++做题,麻烦师傅们再看看。[合集] C++ private member question
[合集] 为什么很少有人用static来实现signleton?请教C++面试题
c++ singleton questionsC++问题几个
How to make sure that I get the original object or only a copy (not original) in such situation?再一个问题c++
another c++ interview questionHow to understand the answer.
[c++] static function in a classC++ online Test 2题
请教goodbug等大神几个AWS的问题弱问,上哪儿去找服务器?
相关话题的讨论汇总
话题: std话题: hash话题: t1话题: tuple话题: tuplet
进入Programming版参与讨论
1 (共1页)
j*****y
发帖数: 22
1
我有以下class:
class T{};
class A{
public:
A(int a, double b, T *t): a_(a), b_(b), t_(t) {}
private:
int a_;
double b_;
T *t_;
};
然后我要create A 的instances如下:
1)如果已经create过了相同的instance with the same a, b and t, return
existing instance (pointer)
2)如果1)不成立create一个新的instance
为了实现1),怎样可以check某一个A的constructor input argument的组合已经见过
了?有什么hash function可以同时hash几个不同type的变量吗?
多谢高手!
y*****g
发帖数: 5
2
如果是C++11或以上的话,可以加一段std::hash>的specialization:
------------------------------------------
namespace std {
template
struct hash> {
template
inline static std::size_t CombineHashes(const TupleT &arg,
std::enable_if_t* = 0) {
std::size_t first_hash = CombineHashes(arg);
std::size_t second_hash = std::hash<
typename std::tuple_element::type>()(std::get(arg)
);
// http://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
return first_hash ^
(second_hash + 0x9e3779b9 + (first_hash << 6) + (first_hash >> 2));
}
template
inline static std::size_t CombineHashes(const TupleT &arg,
std::enable_if_t* = 0) {
return 0;
}
inline std::size_t operator()(const std::tuple &arg) const {
return CombineHashes(arg);
}
};
}
------------------------------------------
然后你就可以用std::unordered_map了:
std::unordered_map, std::unique_ptr>
instances;
N*****m
发帖数: 42603
3
用boost的hash_combine也可以

specialization:

【在 y*****g 的大作中提到】
: 如果是C++11或以上的话,可以加一段std::hash>的specialization:
: ------------------------------------------
: namespace std {
: template
: struct hash> {
: template
: inline static std::size_t CombineHashes(const TupleT &arg,
: std::enable_if_t* = 0) {
: std::size_t first_hash = CombineHashes(arg);
: std::size_t second_hash = std::hash<

j*****y
发帖数: 22
4
多谢大牛们!
s*******i
发帖数: 698
5
搜一下factory design pattern
比如说https://sourcemaking.com/design_patterns/factory_method/cpp/1
S*******s
发帖数: 13043
6
template< class T1, class T2 >
class NamedSingleton{
protected:
NamedSingleton(){};
public:
static T1& GetInstance(const T2& name){
static std::map instances;
typename std::map::iterator ite=instances.find(name);
if(ite==instances.end()){
instances.insert(std::pair(name,T1(name)));
ite=instances.find(name);
}
return (ite->second);
}
private:
};
class A:public NamedSingleton >{
...
}
1 (共1页)
进入Programming版参与讨论
相关主题
弱问,上哪儿去找服务器?How to make sure that I get the original object or only a copy (not original) in such situation?
C++ linking 弱问 (one file)another c++ interview question
C++ class template specialization question[c++] static function in a class
C++ 菜鸟问一个关于template 的问题。请教goodbug等大神几个AWS的问题
请问关于c++实现singleton的问题?how to create an instance of base class in its derived class definition
C++做题,麻烦师傅们再看看。[合集] C++ private member question
[合集] 为什么很少有人用static来实现signleton?请教C++面试题
c++ singleton questionsC++问题几个
相关话题的讨论汇总
话题: std话题: hash话题: t1话题: tuple话题: tuplet