Open In App

Split String by Space into Vector in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite:

Split String By Space Into Vector In C++, so we need to insert every character into a vector of characters. 

Example:

string s=”Geeks for Geeks”

//  Splitting string based on space

// final vector will be [“Geeks” ,”for”,”Geeks”]

We have multiple methods to perform this operation:

  1. Using getline() method
  2. Using string::find_first_not_of
  3. Using the Boost method 

1. Using getline() method

There are various ways in c++ to split the string by spaces or some other mark or symbol. A simple approach can be to iterate over the string and whenever any space comes break the string into the word using the getline() method and store it in the vector. getline() method is a standard library function that is used to take input in multiple lines from the user and also simplify characters from istream object and store them in any array or vector until any delimitation character is not found.

Syntax :

getline(s,t,dl) 

Parameters:

s : s represents the original string

t : t represents the token extracted from the original string

dl : dl represents the delimitation character that splits the string like comma(,),space(” “),etc. 

Examples: 

Input: I am Geek
Output: I
        am
        Geek
        
Input: My name is ABC
Output: My
        name
        is
        ABC

Code:

C++




// C++ Program to implement
// Split String By Space Into Vector
// Using getline() method
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    // String variable i.e, string to be split
    string str = "Geeks for Geeks";
 
    // variable to store token obtained from the original
    // string
    string s;
 
    // constructing stream from the string
    stringstream ss(str);
 
    // declaring vector to store the string after split
    vector<string> v;
 
    // using while loop until the getline condition is
    // satisfied
    // ' ' represent split the string whenever a space is
    // found in the original string
    while (getline(ss, s, ' ')) {
 
        // store token string in the vector
        v.push_back(s);
    }
 
    // print the vector
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << endl;
    }
 
    return 0;
}


Output

Geeks
for
Geeks

2. Using string::find_first_not_of

In this approach, we use the string::find_first_not_of() method, which splits the string according to the given character. string::find_first_not_of() returns the index of the first unmatched character from the given method parameters.

Syntax:

string::find_first_not_of(dl,idx) 

Parameters:

dl : dl represent the delimitation character that splits the string like comma(,),space(” “),etc.

idx : idx represent the index from where the search will start

Example:

C++




// C++ Program implement
// Split String By Space Into Vector
// Using string::find_first_not_of
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    string str = "Geeks for Geeks";
 
    vector<string> v;
 
    // initializing variables
    int start, end;
    start = end = 0;
 
    // defining the delimitation character
    char dl = ' ';
 
    // run the loop until the end variable doesn't reach
    // the end of the string i.e, start = -1
    // string::npos is a constant static member that is
    // defined with -1 str.find_first_not_of(dl, end)) will
    // return the index of first character that is not
    // equal to dl
    while ((start = str.find_first_not_of(dl, end))
           != string::npos) {
        // str.find(dl, start) will return the index of dl
        // from start index
        end = str.find(dl, start);
        // substr function return the substring of the
        // original string from the given starting index
        // to the given end index
        v.push_back(str.substr(start, end - start));
    }
 
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << endl;
    }
 
    return 0;
}


Output :

Geeks
for
Geeks

3. Using the Boost method 

Another way to do this question is by using the boost method. Boost’s string algorithm library contains various functions and one such function is the boost::split function that splits the given string into various parts by the delimitator character provided to the function and stores the substring in the provided data structure.

Syntax :

boost::split(v , s , func) 

Parameters:

 v : v represents any data structure that can store the substrings

 s : original string given by user

 func : function that determines whether the character is deliminator or not

Example:

C++




// C++ Program to implement
// Split String By Space Into Vector
// Using the Boost method
#include <bits/stdc++.h>
 
// header file contain all the functions of boost
#include <boost/algorithm/string.hpp>
using namespace std;
 
int main()
{
    string s = "Geeks for Geeks";
 
    vector<string> v;
 
    // v is the vector to store the substrings after
    // splitting of the original string
    // s is original string
    // is_any_of(" ") returns true or false if space(" ") is
    // present in the string or not
    boost::split(v, s, boost::is_any_of(" "));
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << endl;
    }
 
    return 0;
}


Output : 

Geeks
for
Geeks


Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads