c++charsstdlist

C++ - How to convert char* to std::list<char>


This is a beginner type of question

I'm just wondering if there is a way to convert a null terminated char* to std::list.

Thank you

char* data = ...
std::list<char> clist = convert_chars2list(data);
...
convert_chars2list(char* somedata)
{
    //convert
}

Solution

  • std::list<char> convert_chars2list(char *somedata)
    {
        std::list<char> l;
    
        while(*somedata != 0)
            l.push_back(*somedata++);
    
        // If you want to add a terminating NULL character
        // in your list, uncomment the following statement:
        // l.push_back(0);
    
        return l;
    }