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;
|
string res;
|
||||||
res += string_format("[%s]\n", joinStr(_args, ", ").c_str());
|
res += string_format("[%s]\n", joinStr(_args, ", ").c_str());
|
||||||
res += jsonMPSS(_mpss);
|
res += mapToString<string, string>(_mpss);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
string ArgvContext::operator [](int i)
|
string ArgvContext::operator [](uint i)
|
||||||
{
|
{
|
||||||
if(i < _args.size())
|
if(i < _args.size())
|
||||||
{
|
{
|
||||||
@ -93,6 +93,7 @@ int main(int argc, char** argv)
|
|||||||
cout<<arg.toString()<<endl;
|
cout<<arg.toString()<<endl;
|
||||||
cout<<arg[1]<<endl;
|
cout<<arg[1]<<endl;
|
||||||
cout<<arg["--hehe"]<<endl;
|
cout<<arg["--hehe"]<<endl;
|
||||||
|
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ namespace CPPCOMMON
|
|||||||
~ArgvContext();
|
~ArgvContext();
|
||||||
public:
|
public:
|
||||||
string toString();
|
string toString();
|
||||||
string operator [](int i);
|
string operator [](uint i);
|
||||||
string operator [](const string& key);
|
string operator [](const string& key);
|
||||||
private:
|
private:
|
||||||
vector<string> _args;
|
vector<string> _args;
|
||||||
|
@ -9,35 +9,41 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace CPPCOMMON
|
namespace CPPCOMMON
|
||||||
{
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
inline string jsonMPSS(const map<string, string>& mpss)
|
template<typename T1, typename T2>
|
||||||
{
|
string mapToString(const map<T1, T2>& mp)
|
||||||
if(mpss.empty())
|
|
||||||
{
|
{
|
||||||
return "{}";
|
if(mp.empty())
|
||||||
}
|
{
|
||||||
string res("{");
|
return "{}";
|
||||||
map<string, string>::const_iterator it = mpss.begin();
|
}
|
||||||
res += it->first;
|
stringstream ss;
|
||||||
res += ": ";
|
ss<<'{';
|
||||||
res += it->second;
|
typename map<T1, T2>::const_iterator it = mp.begin();
|
||||||
it++;
|
ss<<it->first<<": "<<it->second;
|
||||||
while(it != mpss.end())
|
|
||||||
{
|
|
||||||
res += ", ";
|
|
||||||
res += it->first;
|
|
||||||
res += ": ";
|
|
||||||
res += it->second;
|
|
||||||
it++;
|
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>
|
template<class kT, class vT>
|
||||||
void printMap(const map<kT, vT>& mp)
|
void printMap(const map<kT, vT>& mp)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user