1

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
}

6 Answers 6

9

This is probably the simplest way:

#include <list>
#include <string>

int main() 
{ 
    char const* data = "Hello world";
    std::list<char> l(data, data + strlen(data));
}

It exploits the fact that std::string has an interface which is compatible with STL containers.

7
  • A better solution that mine. Feb 22, 2013 at 23:10
  • That works? I would have thought you needed to use iterators.
    – chris
    Feb 22, 2013 at 23:15
  • Doing it this way does give a warning with vs2010. warning C4930: 'std::list<_Ty> l(std::string)': prototyped function not called (was a variable definition intended?)
    – Caesar
    Feb 22, 2013 at 23:16
  • @Caesar, Yeah, that's a sign of the MVP. l is a function with that declaration.
    – chris
    Feb 22, 2013 at 23:17
  • 1
    @Caesar: That's correct. In fact, I wrote this is probably the "simplest" way. I believe you should always favor simplicity, unless you are really concerned with performance. There is no evidence here that this would be a performance-critical operation. Hence, my choice.
    – Andy Prowl
    Feb 22, 2013 at 23:24
2
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;
}
11
  • 1
    I got it wrong too, even after looking it up! The moral is that you basically shouldn't do things like this because it's too easy to misunderstand; however, this is an exception because it's a common idiom that programmers know works even if they don't know all the rules that make it work off the top of their heads. Feb 22, 2013 at 23:49
  • 2
    @AndyProwl: It doesn't matter when the side-effect happens. It has no effect on the result. Feb 22, 2013 at 23:50
  • 2
    @DavidSchwartz: Oh, I got it. operator * does not perform a value computation that depends on the value of the object somedata. The value computation depends on the value returned by somedata++. So that's not UB. Damn, I think I'll never learn this stuff
    – Andy Prowl
    Feb 22, 2013 at 23:55
  • 2
    @NikBougalis: Don't do that: you'll most likely get stoned by people pissed off at how many times these things have been repeated over and over again :-)
    – Andy Prowl
    Feb 22, 2013 at 23:56
  • 1
    Woe... my dreams of a "Famous Question" gold badge evaporated. Feb 23, 2013 at 0:06
1
std::list<char> convert_chars2list(char* somedata)
{
  std::list<char> res;
  while (*somedata)
     res.push_back(*somedata++);
  return res;
}
0
0

If your char* is a C-style string than you can do this (that means it ends with a \0)

#include <list>

int main()
{
    char hello[] = "Hello World!";

    std::list<char> helloList;

    for(int index = 0; hello[index] != 0; ++index)
    {
        helloList.push_back( hello[index] );
    }    
}
0

Let C++ standard library take care of pointer handling and iterator traversing.

std::string str = "Hello world";

Though you can access each character with [] operator from std::string itself, if you want a list of characters

std::list<char> chars(str.begin(), str.end());
-1

It's really weird to use a std::list to contain characters, if you really want to use stl to contain characters, you could use std::string, which would allow you to do:

char*           data = ....;
std::string     string(data);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.