Created
March 23, 2020 10:28
-
-
Save blockspacer/c457e1c825610e09c053edc0ec421e6d to your computer and use it in GitHub Desktop.
C++ class in std::map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Online C++ Compiler. | |
Code, Compile, Run and Debug C++ program online. | |
Write your code in this editor and press "Run" button to compile and execute it. | |
*******************************************************************************/ | |
#include <iostream> | |
#include <functional> | |
#include <ostream> | |
#include <string> | |
#include <map> | |
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname) \ | |
Classname(const Classname&) = default; \ | |
Classname& operator=(const Classname&) = default; \ | |
Classname(Classname&&) = default; \ | |
Classname& operator=(Classname&&) = default; \ | |
/* Fails at compile-time if default-copy doesn't work. */ \ | |
static void DRAKE_COPYABLE_DEMAND_COPY_CAN_COMPILE() { \ | |
(void) static_cast<Classname& (Classname::*)( \ | |
const Classname&)>(&Classname::operator=); \ | |
} | |
namespace drake { | |
namespace solvers { | |
/// Identifies a MathematicalProgramSolverInterface implementation. | |
/// | |
/// A moved-from instance is guaranteed to be empty and will not compare equal | |
/// to any non-empty ID. | |
class SolverId { | |
public: | |
SolverId() = default; | |
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(SolverId); | |
~SolverId() = default; | |
/// Constructs a specific, known solver type. Internally, a hidden | |
/// identifier is allocated and assigned to this instance; all instances that | |
/// share an identifier (including copies of this instance) are considered | |
/// equal. The solver names are not enforced to be unique, though we | |
/// recommend that they remain so in practice. | |
explicit SolverId(std::string name); | |
//explicit SolverId(const SolverId& rhs); | |
const std::string& name() const { return name_; } | |
// comparator function object | |
/*bool operator() | |
(const SolverId& lhs | |
, const SolverId& rhs) const | |
{ | |
return lhs.name_ < rhs.name_; | |
}*/ | |
bool operator< | |
(const SolverId& rhs) const | |
{ | |
return name_ < rhs.name_; | |
} | |
private: | |
friend bool operator==(const SolverId&, const SolverId&); | |
friend bool operator!=(const SolverId&, const SolverId&); | |
friend struct std::less<SolverId>; | |
std::string name_; | |
}; | |
bool operator==(const SolverId&, const SolverId&); | |
bool operator!=(const SolverId&, const SolverId&); | |
std::ostream& operator<<(std::ostream&, const SolverId&); | |
/////////// | |
SolverId::SolverId(std::string name) | |
: name_{std::move(name)} {} | |
//SolverId::SolverId(const SolverId& rhs) | |
// : name_{rhs.name_} {} | |
bool operator==(const SolverId& a, const SolverId& b) { | |
return a.name_ == b.name_; | |
} | |
bool operator!=(const SolverId& a, const SolverId& b) { | |
return a.name_ != b.name_; | |
} | |
std::ostream& operator<<(std::ostream& os, const SolverId& self) { | |
// N.B. The ID is _not_ exposed to callers. | |
os << self.name(); | |
return os; | |
} | |
} // namespace solvers | |
} // namespace drake | |
int main() | |
{ | |
drake::solvers::SolverId id1{"Hello"}; | |
drake::solvers::SolverId id2{"World"}; | |
std::map<drake::solvers::SolverId, drake::solvers::SolverId> mapper; | |
mapper[id1] = id2; | |
std::cout<<"Hello World"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment