mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon
This commit is contained in:
parent
ff34095252
commit
8f06d1340a
@ -54,11 +54,11 @@ namespace CPPCOMMON
|
||||
{
|
||||
string res;
|
||||
res += string_format("[%s]\n", joinStr(_args, ", ").c_str());
|
||||
res += jsonMPSS(_mpss);
|
||||
res += mapToString<string, string>(_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<<arg.toString()<<endl;
|
||||
cout<<arg[1]<<endl;
|
||||
cout<<arg["--hehe"]<<endl;
|
||||
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ namespace CPPCOMMON
|
||||
~ArgvContext();
|
||||
public:
|
||||
string toString();
|
||||
string operator [](int i);
|
||||
string operator [](uint i);
|
||||
string operator [](const string& key);
|
||||
private:
|
||||
vector<string> _args;
|
||||
|
@ -9,35 +9,41 @@
|
||||
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
inline string jsonMPSS(const map<string, string>& mpss)
|
||||
{
|
||||
if(mpss.empty())
|
||||
template<typename T1, typename T2>
|
||||
string mapToString(const map<T1, T2>& mp)
|
||||
{
|
||||
return "{}";
|
||||
}
|
||||
string res("{");
|
||||
map<string, string>::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<T1, T2>::const_iterator it = mp.begin();
|
||||
ss<<it->first<<": "<<it->second;
|
||||
it++;
|
||||
while(it != mp.end())
|
||||
{
|
||||
ss<<", "<<it->first<<": "<<it->second;
|
||||
it++;
|
||||
}
|
||||
ss<<"}";
|
||||
return ss.str();
|
||||
}
|
||||
res += "}";
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
template<typename T1, typename T2>
|
||||
string pairToString(const pair<T1, T2>& p)
|
||||
{
|
||||
stringstream ss;
|
||||
ss<<p.first<<":"<<p.second;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<class kT, class vT>
|
||||
void printMap(const map<kT, vT>& mp)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user