Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Custorm string

#include <iostream>
#include <string>
#include <memory>

using namespace std;

typedef unsigned char byte;

class styledChar
{
    public:
        styledChar() {};
        styledChar(wchar_t chr): _chr(chr), _styleIndex(0) {};
        wchar_t GetChar() const { return _chr; };
        byte GetStyleIndex() const { return _styleIndex; };
        void SetStyleIndex(byte styleIndex) { _styleIndex = styleIndex; };
    private:
        wchar_t _chr;
        byte _styleIndex;
};

class styledString: public basic_string<styledChar>
{
    public:
        styledString(const wstring& right) : basic_string<styledChar>(CreateBuf(right).get(), right.length())
        {
        }
    
    private:
        static unique_ptr<styledChar[]> CreateBuf(const wstring& right)
        {
            unique_ptr<styledChar[]> buf(new styledChar[right.length()]);
            int i = 0;
            for (auto r : right)
            {
                styledChar ch(r);
                buf[i++] = ch;
            }
            return buf;
        }
        
    friend wostream& operator<< (wostream &out, const styledString &str)
    {
        for(auto c : str)
        {
            out << c.GetChar();
        }       
        return out;
    };
};

int main()
{
   styledString s(L"test string");
   

   wcout << s << endl; 
   
   return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.