From 8f06d1340adb0332527bae7b455c05041ed91403 Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Sun, 18 Aug 2013 23:06:18 +0800 Subject: [PATCH] update cppcommon --- src/cppcommon/argv_functs.cpp | 5 ++-- src/cppcommon/argv_functs.h | 2 +- src/cppcommon/map_functs.h | 48 ++++++++++++++++++++--------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/cppcommon/argv_functs.cpp b/src/cppcommon/argv_functs.cpp index 5ddf55c..8b7d17f 100644 --- a/src/cppcommon/argv_functs.cpp +++ b/src/cppcommon/argv_functs.cpp @@ -54,11 +54,11 @@ namespace CPPCOMMON { string res; res += string_format("[%s]\n", joinStr(_args, ", ").c_str()); - res += jsonMPSS(_mpss); + res += mapToString(_mpss); return res; } - string ArgvContext::operator [](int i) + string ArgvContext::operator [](uint i) { if(i < _args.size()) { @@ -93,6 +93,7 @@ int main(int argc, char** argv) cout<(pair(1,1.2))< _args; diff --git a/src/cppcommon/map_functs.h b/src/cppcommon/map_functs.h index 6a6707d..f7775bd 100644 --- a/src/cppcommon/map_functs.h +++ b/src/cppcommon/map_functs.h @@ -9,35 +9,41 @@ #include #include +#include namespace CPPCOMMON { using namespace std; - inline string jsonMPSS(const map& mpss) - { - if(mpss.empty()) + template + string mapToString(const map& mp) { - return "{}"; - } - string res("{"); - map::const_iterator it = mpss.begin(); - res += it->first; - res += ": "; - res += it->second; - it++; - while(it != mpss.end()) - { - res += ", "; - res += it->first; - res += ": "; - res += it->second; + if(mp.empty()) + { + return "{}"; + } + stringstream ss; + ss<<'{'; + typename map::const_iterator it = mp.begin(); + ss<first<<": "<second; it++; + while(it != mp.end()) + { + ss<<", "<first<<": "<second; + it++; + } + ss<<"}"; + return ss.str(); } - res += "}"; - return res; - } - + + template + string pairToString(const pair& p) + { + stringstream ss; + ss< void printMap(const map& mp) {