# std::pair 他的實作很簡單,幾乎跟我下面的實作一樣,只是他多了swap()跟比較運算子 < <= > => == != operator overlading,讓pair可以互相比較,先比較key再比較value [C++ STL pair用法详解 - C语言中文网](https://c.biancheng.net/view/7169.html) ```cpp= #include <utility> #include <iostream> #include <string> template <typename T1, typename T2> class Pair { public: Pair() : first(T1()), second(T2()) {} Pair(T1 first, T2 second) : first(first), second(second) {} T1 first; T2 second; }; int main() { Pair<std::string, int> my_pair("my_pair1", 1); std::cout << my_pair.first << " " << my_pair.second << std::endl; std::pair <std::string, int> pair1("pair1", 1); std::cout << pair1.first << " " << pair1.second << std::endl; return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up