15

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

}
Community
  • 1
  • 1
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65

7 Answers7

23
#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:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Can you provide a link to a discussion on why it is a bad practise to use `using namespace x`? – jli Dec 09 '11 at 16:12
  • @jli: Added the link to my answer. See it. – Nawaz Dec 09 '11 at 16:13
  • 2
    @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!!!!! – Ahoura Ghotbi Dec 09 '11 at 16:29
6
vector<string> v;
boost::split(v, line, ::isspace);

http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
5

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;
}
Newbyte
  • 2,421
  • 5
  • 22
  • 45
Vijay
  • 65,327
  • 90
  • 227
  • 319
3

Try strtok. Look for it in the C++ reference:.

pppery
  • 3,731
  • 22
  • 33
  • 46
LucianMLI
  • 183
  • 1
  • 11
  • 3
    `strtok` is a C library thing, whereas the poster is asking for how to do it correctly with C++. – jli Dec 09 '11 at 16:08
  • 2
    and 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)? – LucianMLI Dec 12 '11 at 08:58
  • If you mix them, you add unnecessary dependances, among other issues. – jli Dec 12 '11 at 14:34
  • A 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++ . – LucianMLI Dec 12 '11 at 18:21
  • http://stackoverflow.com/questions/4025869/using-mixing-c-in-c-code I guess the dependencies is not really the issue, but imo doing something like `#include ` `#include ` gets redundant. – jli Dec 12 '11 at 18:29
  • Also I just realized I spelled dependencies wrong in my second comment (damn phone soft keyboards). – jli Dec 12 '11 at 18:32
1

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;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

C++ is best used with the almost-standard-library boost.

And an example : http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768

Flexo
  • 87,323
  • 22
  • 191
  • 272
Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
0

Either use a stringstream or read token by token from your ifstream.

To do it with a stringstream:

string line, token;
ifstream in(file);

while(getline(in, line))
{
    stringstream s(line);
    while (s >> token)
    {
        // save token to your array by value
    }
}
jli
  • 6,523
  • 2
  • 29
  • 37
  • Of course you could use boost if you so desire, or another STL function to do the copying out of the stringstream. – jli Dec 09 '11 at 16:10
  • If 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. – Cubbi Dec 09 '11 at 16:27
  • This is true. May as well edit to use that method. – jli Dec 09 '11 at 16:28