The tostring of C ++ is very troublesome.

Source: Internet
Author: User

Effect:

//main.cpp :: main()map<char, vector<bool> > temp= make_test_val();cout<< toString(temp);

Use the template to implement tostring (any type) and then it is convenient

The following code is used:

//tools.h#include<map>#include<vector>using namespace std;//---toString---//builderclass tostring_bulider{public:    string _str;    string _prefix;    string _comma;    string _suffix;    bool first_item;    tostring_bulider(){        first_item= true;        _prefix= "[";        _suffix= "]";        _comma= ", ";    }    void prefix(){        _str+= _prefix;    }    template<typename T>    void push(const T& item){        if(first_item){            first_item= false;        }else{            _str+= _comma;        }        _str+= ::toString(item);    }    void suffix(){        _str+= _suffix;    }    string toString()const{return _str;}};class tostring_map_bulider{public:    string _str;    string _prefix;    string _comma;    string _eq;    string _suffix;    bool first_item;    tostring_map_bulider(){        first_item= true;        _prefix= "{";        _suffix= "}";        _comma= ", ";        _eq= ":";    }    void prefix(){        _str+= _prefix;    }    template<typename T>    void push(const T& item){        if(first_item){            first_item= false;        }else{            _str+= _comma;        }        _str+= ::toString(item.first)+ _eq+ ::toString(item.second);    }    void suffix(){        _str+= _suffix;    }    string toString()const{return _str;}};//etc.//default typestring toString(const string& s){return s;}//system typetemplate<typename T>string toString(const T& v){    return v.toString();}template<>string toString<char>(const char& v){    return string()+v;}template<>string toString<bool>(const bool& v){    return v? "true": "false";}//etc.//Containertemplate<typename T, typename Tfmt>string toString(const T& v, Tfmt fmter){    fmter.prefix();    for(auto iter = begin(v); iter!=end(v); ++iter)        fmter.push(*iter);     fmter.suffix();    return move(toString(fmter));}//std Containertemplate<typename T>string toString(const vector<T>& v){    return toString(v,tostring_bulider());}template<typename Tk, typename Tv>string toString(const map<Tk,Tv>& v){    return toString(v, tostring_map_bulider());}//etc.

However, it is shown as follows:

{a:[false, false, true, false], b:[false, false, true, false, true, false, true, false]}

You can add the following code:

//main.cppclass my_tostring_map_bulider: public tostring_map_bulider{public:string toString_vector_bool(const vector<bool>& v){string ret;for(auto iter= begin(v); iter!=end(v); ++iter)ret+= *iter? "1": "0";return ret;}void push(const pair<char, vector<bool> >& item){if(first_item){first_item= false;}else{_str+= _comma;}_str+= ::toString(item.first)+ _eq+ toString_vector_bool(item.second);}};

Then use the following code:

//main.cpp :: main()map<char, vector<bool> > temp= make_test_val();cout<< toString(temp, my_tostring_map_bulider());

The output is as follows:

{a:0010, b:00101010}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.