Sai A Sai A
Updated date Jan 03, 2024
In this blog, we will learn various techniques to convert strings to lists in C++ with practical examples and detailed explanations. Explore loop-based methods, the copy algorithm, and the stringstream approach to permit your C++ programming skills.

Introduction:

In C++, strings and lists are fundamental data types that serve different purposes. However, there are scenarios where you might need to convert a string into a list or an array for easier manipulation of individual elements. In this blog, we will explore multiple methods to convert a string to a list in C++

Method 1: Using a For Loop

#include <iostream>
#include <string>
#include <vector>

int main() {
    // Step 1: Input a string
    std::string inputString = "Hello,World";

    // Step 2: Initialize an empty vector to store individual characters
    std::vector<char> charList;

    // Step 3: Use a for loop to iterate through each character in the string
    for (char c : inputString) {
        // Step 4: Add each character to the vector
        charList.push_back(c);
    }

    // Step 5: Output the resulting list
    std::cout << "Method 1 Output: ";
    for (char c : charList) {
        std::cout << c << " ";
    }

    return 0;
}

Output:

Method 1 Output: H e l l o , W o r l d

In this method, we use a for loop to iterate through each character in the input string. For each character, we append it to a vector (charList). Finally, we output the resulting list.

Method 2: Using the copy Algorithm

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

int main() {
    // Step 1: Input a string
    std::string inputString = "Hello,World";

    // Step 2: Initialize an empty vector
    std::vector<char> charList;

    // Step 3: Use the copy algorithm to copy characters from the string to the vector
    std::copy(inputString.begin(), inputString.end(), std::back_inserter(charList));

    // Step 4: Output the resulting list
    std::cout << "Method 2 Output: ";
    for (char c : charList) {
        std::cout << c << " ";
    }

    return 0;
}

Output:

Method 2 Output: H e l l o , W o r l d

In this method, we utilize the copy algorithm from the C++ Standard Template Library (STL) to copy characters from the input string to the vector (charList). The std::back_inserter function is used to insert elements at the end of the vector.

Method 3: Using stringstream

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main() {
    // Step 1: Input a string
    std::string inputString = "Hello,World";

    // Step 2: Initialize a string stream with the input string
    std::istringstream iss(inputString);

    // Step 3: Initialize an empty vector
    std::vector<std::string> stringList;

    // Step 4: Use a while loop to extract individual words from the string stream
    std::string word;
    while (iss >> word) {
        // Step 5: Add each word to the vector
        stringList.push_back(word);
    }

    // Step 6: Output the resulting list
    std::cout << "Method 3 Output: ";
    for (const auto& word : stringList) {
        std::cout << word << " ";
    }

    return 0;
}

Output:

Method 3 Output: Hello,World

In this method, we employ a std::stringstream to convert the input string into a stream of words. Using a while loop, we extract individual words from the stream and add them to a vector (stringList). Finally, we output the resulting list of words.

Conclusion:

In this blog, we talked about different ways to change a string into a list in C++. Whether you like using a loop, the copy method, or a stringstream, the important thing is to know the advantages and disadvantages of each and pick the one that works best for your situation.

Comments (0)

There are no comments. Be the first to comment!!!