Common c++ STL cout

For Leetcode debugging use.

// stl Template cout
template <typename T1, typename T2> 
ostream& operator<<(ostream& os, const std::pair<T1,T2> &p) { 
    os << "{" << p.first << "," << p.second << "}"; 
    return os; 
} 
template <typename T> 
ostream& operator<<(ostream& os, const std::vector<T>& v) {  
    os << "["; 
    for (int i = 0; i < v.size(); ++i) {
        os << v[i]; 
        if (i != v.size() - 1) 
            os << ", "; 
    }  
    os << "]" << endl; return os; 
} 
template <typename T> 
ostream& operator<<(ostream& os, const std::set<T>& v) {
    os << "{"; 
    auto itr = v.begin(); 
    for(int i = 0; i < v.size(); ++i, itr++) { 
        os << *itr; 
        if (i != v.size() - 1) 
            os << ", "; 
    }  
    os << "}" << endl; 
    return os; 
} 
template <typename T> 
ostream& operator<<(ostream& os, const std::unordered_set<T>& v) {  
    os << "{"; 
    auto itr = v.begin(); 
    for(int i = 0; i < v.size(); ++i, itr++) {
    os << *itr; 
    if (i != v.size() - 1) 
        os << ", "; 
    }
    os << "}" << endl; 
    return os; 
}
template <typename T1, typename T2> 
ostream& operator<<(ostream& os, const std::map<T1, T2>& v) {  
    os << "{"; 
    auto itr = v.begin(); 
    for(int i = 0; i < v.size(); ++i, itr++) { 
        os << itr->first << ": " << itr->second; 
        if (i != v.size() - 1) 
        os << ", "; 
    }  
    os << "}" << endl; 
    return os; 
} 
template <typename T1, typename T2> 
ostream& operator<<(ostream& os, const std::multimap<T1, T2>& v) {  
    os << "{"; 
    auto itr = v.begin(); 
    for(int i = 0; i < v.size(); ++i, itr++) { 
        os << itr->first << ": " << itr->second; 
        if (i != v.size() - 1) 
            os << ", "; 
    }  
    os << "}" << endl; 
    return os; 
} 
template <typename T> 
ostream& operator<<(ostream& os, const std::queue<T>& v) {  
    os << "["; 
    auto copy = v;
    while (!copy.empty()) {
        auto e = copy.front();
        copy.pop();
        cout << e;
        if (copy.size()) 
            cout << ", ";
    }
    os << "]" << endl; return os; 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

one × 3 =

Note: Commenter is allowed to use '@+User+:' to automatically notify your reply to other commenter.