How to Split a C++ String into a Vector of Substrings? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, splitting a string into a vector of substrings means we have to split the given string into a substring based on a given delimiter and store each substring in a vector. In this article, we will learn how to split a string into a vector of substrings in C++. Example: Input: str= "Hello, I am Geek from Geeksforgeeks "Delimiter= ' 'Output:Hello,IamGeekfromGeeksforgeeksSplit an std::string into a Vector of Strings in C++To split a std::string into a std::vector of substrings use the stringstream with a combination of std::string::getline. The below steps shows how to do that: ApproachCreate an input string stream from the input string using the stringstream.Iterate through the stream, using getline to extract the substring using the delimiter.Add the extracted substring to the vector.Print the vector of substrings.C++ Program to Split a String into Vector of SubstringsThe below example demonstrates how we can split a given string into a vector of substrings in C++. C++ // C++ Program to illustrate how to split a string to vector // of substring #include <iostream> #include <iterator> #include <sstream> #include <string> #include <vector> using namespace std; // Function to split a string into tokens based on a // delimiter vector<string> splitString(string& input, char delimiter) { // Creating an input string stream from the input string istringstream stream(input); // Vector to store the tokens vector<string> tokens; // Temporary string to store each token string token; // Read tokens from the string stream separated by the // delimiter while (getline(stream, token, delimiter)) { // Add the token to the vector of tokens tokens.push_back(token); } // Return the vector of tokens return tokens; } int main() { // Input string string input = "Hello, I am Geek from Geeksforgeeks"; // Delimiter char delimiter = ' '; // calling the function to Split the input string into // vector of substring vector<string> vecOfSubStr = splitString(input, delimiter); // Print the vector of substrings for (auto& it : vecOfSubStr) { cout << it << endl; } return 0; } OutputHello, I am Geek from Geeksforgeeks Time complexity: O(n), here n is the length of the input string.Auxilliary Space: O(n) Comment More infoAdvertise with us Next Article How to Split a C++ String into a Vector of Substrings? B bhushanc2003 Follow Improve Article Tags : C++ Programs C++ cpp-vector cpp-strings CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads C++ Program to Split a String Into a Number of Sub-Strings Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C++Note: The main disadvantage of strtok() 3 min read How to Declare Pointer to an Array of Strings in C++? In C++, an array of a string is used to store multiple strings in contiguous memory locations and is commonly used when working with collections of text data. In this article, we will learn how to declare a pointer to an array of strings in C++. Pointer to Array of String in C++If we are working wit 2 min read How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read How to Find and Replace All Occurrences of a Substring in a C++ String? In C++, strings are sequences of characters that are used to represent textual data. In this article, we will learn how to find and replace all the occurrences of a particular substring in the given string. For Example, Input: str = "Lokesh is a good programmer, but Lokesh is still in the learning p 2 min read C++ Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 4 min read Convert String to Integer Vector in C++ Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve 2 min read How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee 2 min read Find if a String ends With the Given Substring in C++ You are given two strings. The task is to check if the main string ends with another string in C++. Example Input: mainString = "Hello! Geek"; targetString = "Geek" Output: Hello! Geek ends with Geek.We can find if the given string ends with the target string using the following method: Checking if 2 min read How to Take Multiple String Inputs in a Single Line in C++? In C++, strings are used to store textual data. While working with user input, we often may need to take multiple string inputs in a single line only. In this article, we will learn how to take multiple string inputs in a single line. Example Input: Hi,Geek,Welcome,to,GfG delimiter = ',' Output: Str 2 min read How to Compare Two Substrings in a Character Array in C++? In C++, character arrays are used to store a sequence of characters also known as strings. A Substring is a continuous sequence of characters within a string. In this article, we will learn how we can compare two substrings in a character array in C++. Examples: Input: string: "Hello World" Substrin 2 min read Like