7

I have string with number on ints seperated by space delimiter. Can some one help me how to split the string into ints. I tried to use find and then substr. Is there a better way to do it ?

brett
  • 5,379
  • 12
  • 43
  • 48
  • I am unsure what exact format you are describing - just space-seperated numbers? An example string would help. – Georg Fritzsche Aug 06 '10 at 07:33
  • Google Search "splitting string c++" gives: http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html – Akusete Aug 06 '10 at 07:33

4 Answers4

9

Use a stringsteam:

#include <string>
#include <sstream>

int main() {
    std::string s = "100 123 42";
    std::istringstream is( s );
    int n;
    while( is >> n ) {
         // do something with n
    }
}
2

This has been discussed as part of Split a string in C++?

Also, you can use boost library split function to achieve the splitting without a loop in your program. Eg.

boost::split(epoch_vector, epoch_string, boost::is_any_of(","));

Community
  • 1
  • 1
Kisalay
  • 607
  • 1
  • 6
  • 11
1

A version using boost. The stringstream version from Neil is so much simpler!

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>

int main()
{
  const std::string str( "20 30 40 50" );
  std::vector<int> numbers;
  boost::tokenizer<> tok(str);
  std::transform( tok.begin(), tok.end(), std::back_inserter(numbers), 
                  &boost::lexical_cast<int,std::string> );
  // print them
  std::copy( numbers.begin(), numbers.end(), std::ostream_iterator<int>(std::cout,"\n") ); 
}
Brian O'Kennedy
  • 1,721
  • 1
  • 13
  • 18
  • 1
    You don't even need boost for this. You can simply construct an instance of `std::istringstream is(str);` and then do `std::copy(std::istream_iterator(is), std::istream_iterator(), std::back_inserter(numbers));` – reko_t Aug 06 '10 at 08:13
  • @reko_t Nice! Does the istream_iterator expect space delimited items, or can it also handle other delimiters? – Brian O'Kennedy Aug 06 '10 at 08:23
  • It expects space delimited items, it can't handle other delimiters. – reko_t Aug 06 '10 at 08:42
0

I had some trouble when reading and converting more than one string (I found I had to clear the string stream). Here a test I made with multiple int/string conversions with read/write to an i/o file.

#include <iostream>
#include <fstream> // for the file i/o
#include <string>  // for the string class work
#include <sstream> // for the string stream class work

using namespace std;

int main(int argc, char *argv[])
{
    // Aux variables:
    int aData[3];
    string sData;
    stringstream ss;

    // Creation of the i/o file:
    // ...
    // Check for file open correctly:
    // ...

    // Write initial data on file:
    for (unsigned i=0; i<6; ++i)
    {
        aData[0] = 1*i;
        aData[1] = 2*i;
        aData[2] = 3*i;

        ss.str(""); // Empty the string stream
        ss.clear();
        ss << aData[0] << ' ' << aData[1] << ' ' << aData[2];
        sData = ss.str(); // number-to-string conversion done

        my_file << sData << endl;
    }

    // Simultaneous read and write:
    for (unsigned i=0; i<6; ++i)
    {
        // Read string line from the file:
        my_file.seekg(0, ios::beg);
        getline (my_file, sData); // reads from start of file

        // Convert data:
        ss.str(""); // Empty the string stream
        ss.clear();
        ss << sData;
        for (unsigned j = 0; j<3; ++j)
            if (ss >> aData[j]) // string-to-num conversion done
                ;

        // Write data to file:
        my_file.seekp(0, ios::end);
        my_file << 100+aData[0] << ' '; // appends at the end of stream.
        my_file << 100+aData[1] << ' ';
        my_file << 100+aData[2] << endl;
    } 
    // R/W complete.

    // End work on file:
    my_file.close();

    cout << "Bye, world! \n";

    return 0;
}
RiGonz
  • 461
  • 4
  • 8