This post will explore how to convert a std::string to a std::list of chars in C++.

1. Naive Solution

A naive solution is to use a range-based for-loop or the standard loop to push_back all characters of the std::string individually to the std::list.

Download  Run Code

Output:

A
B
C

2. Using Range Constructor

The recommended approach is to use the range constructor of the list class, which takes two input iterators. For converting a std::string to std::list of chars, we have to pass the input iterator to the beginning and the end of the std::string, as shown below:

Download  Run Code

Output:

A
B
C

3. Using std::copy function

Another good alternative is to use the standard algorithm std::copy for copying characters of the std::string to the end of a std::list using std::back_inserter, as shown below:

Download  Run Code

Output:

A
B
C

 
The std::back_inserter internally uses std::push_back function. If std::list has sufficient memory to accommodate all characters of the std::string, we can even pass the input iterator to the beginning of std::list:

Download  Run Code

Output:

A
B
C

That’s all about converting a std::string to a std::list of chars in C++.