Split String by Space into Vector in C++ STL
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:
- Using getline() method
- Using string::find_first_not_of
- 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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "Geeks for Geeks" ;
string s;
stringstream ss(str);
vector<string> v;
while (getline(ss, s, ' ' )) {
v.push_back(s);
}
for ( int i = 0; i < v.size(); i++) {
cout << v[i] << endl;
}
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "Geeks for Geeks" ;
vector<string> v;
int start, end;
start = end = 0;
char dl = ' ' ;
while ((start = str.find_first_not_of(dl, end))
!= string::npos) {
end = str.find(dl, start);
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++
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
string s = "Geeks for Geeks" ;
vector<string> v;
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
Share your thoughts in the comments
Please Login to comment...