Split a string into an array in C++
Solution 1
#include <sstream> //for std::istringstream
#include <iterator> //for std::istream_iterator
#include <vector> //for std::vector
while(std::getline(in, line))
{
std::istringstream ss(line);
std::istream_iterator<std::string> begin(ss), end;
//putting all the tokens in the vector
std::vector<std::string> arrayTokens(begin, end);
//arrayTokens is containing all the tokens - use it!
}
By the way, use qualified-names such as std::getline
, std::ifstream
like I did. It seems you've written using namespace std
somewhere in your code which is considered a bad practice. So don't do that:
Solution 2
vector<string> v;
boost::split(v, line, ::isspace);
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
Solution 3
I have written a function for a similar requirement of mine, maybe you can use it!
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s+' ');
std::string item;
while(std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
Solution 4
Try strtok
. Look for it in the C++ reference:.
Solution 5
The code below uses strtok()
to split a string into tokens and stores the tokens in a vector.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
char one_line_string[] = "hello hi how are you nice weather we are having ok then bye";
char seps[] = " ,\t\n";
char *token;
int main()
{
vector<string> vec_String_Lines;
token = strtok( one_line_string, seps );
cout << "Extracting and storing data in a vector..\n\n\n";
while( token != NULL )
{
vec_String_Lines.push_back(token);
token = strtok( NULL, seps );
}
cout << "Displaying end result in vector line storage..\n\n";
for ( int i = 0; i < vec_String_Lines.size(); ++i)
cout << vec_String_Lines[i] << "\n";
cout << "\n\n\n";
return 0;
}
Ahoura Ghotbi
Updated on November 16, 2020Comments
-
Ahoura Ghotbi over 3 years
Possible Duplicate:
How to split a string in C++?I have an input file of data and each line is an entry. in each line each "field" is seperated by a white space " " so I need to split the line by space. other languages have a function called split (C#, PHP etc) but I cant find one for C++. How can I achieve this? Here is my code that gets the lines:
string line; ifstream in(file); while(getline(in, line)){ // Here I would like to split each line and put them into an array }
-
jli over 12 years
strtok
is a C library thing, whereas the poster is asking for how to do it correctly with C++. -
jli over 12 yearsOf course you could use boost if you so desire, or another STL function to do the copying out of the stringstream.
-
jli over 12 yearsCan you provide a link to a discussion on why it is a bad practise to use
using namespace x
? -
Nawaz over 12 years@jli: Added the link to my answer. See it.
-
Cubbi over 12 yearsIf the input ends in whitespace, this inner while-loop generates an additional empty token in the end. The idiomatic C++
while(s >> token)
does not. -
jli over 12 yearsThis is true. May as well edit to use that method.
-
Ahoura Ghotbi over 12 years@Nawaz thanks, Looking at my other questions, the syntax I am using and the way I am learning C++ from my instructors at uni is highly questionable :S!!!!!
-
LucianMLI over 12 yearsand c++ is not c?(... OMG all those years they all lied to me :D). Since when c library have stopped working in c++(or turned incorrect)?
-
jli over 12 yearsIf you mix them, you add unnecessary dependances, among other issues.
-
LucianMLI over 12 yearsA link to the issues and DEPENDENCIES involved in using c in c++ please? ...I mean for all those years of wrongly compiling and using c code and libraries in c++ .
-
jli over 12 yearsstackoverflow.com/questions/4025869/using-mixing-c-in-c-code I guess the dependencies is not really the issue, but imo doing something like
#include <iostream>
#include <cstdio>
gets redundant. -
jli over 12 yearsAlso I just realized I spelled dependencies wrong in my second comment (damn phone soft keyboards).