JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (oldest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
Re: 2.1. Parsing String Input (response to post by orchidmajumder) | Reply
orchidmajumber, i know this thread isn't the place for this. But I would like some advice and help with a dynamic programming problems. Can you help me? please e-mail me oliversun93@gmail.com
Re: 2.1. Parsing String Input (response to post by Nickolas) | Reply
I think using stringstream (with the ignore function) may come handy for splitting strings delimited by space,comma or any character delimiter.
#include<cstdio>
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
 
template<class T> vector<T> split(string s,char c=' ') {
    stringstream A(s);
	vector<T> res;
	T t;
	while (A>>t) {
        res.push_back(t);
        if(A.peek()==c)
            A.ignore();
	}
	return res;
}
 
int main() {
    string s;
    getline(cin,s);
    vector<int>d=split<int>(s,',');
    for(int i=0;i<d.size();i++)
        cout<<d[i]<<"  ";
    return 0;
}
Re: 2.1. Parsing String Input (response to post by Nickolas) | Reply
That's why I use Java :P
Re: 2.1. Parsing String Input (response to post by Nickolas) | Reply
I can't make it any better. split function has STL input and output, and must work properly for arbitrary string length. That's why it is so ugly.

As I've said, the main goal is to show that there is a cool strtok library function.
Re: 2.1. Parsing String Input (response to post by syg96) | Reply
Do you mind just posting the proper C-styled way to write split() using strtok?
Re: 2.1. Parsing String Input (response to post by Nickolas) | Reply
I'm sorry but: now it looks even worse=) Though is it correct now.

Personally I prefer not to mess with manual dynamic allocation in olympic programming=) Only C++ containers and fixed-size arrays.

I think you can leave it as it is now. Coders who prefer C-style would do it in proper way themselves. The key idea is to show strtok, not C string and deallocation routines=)
Re: 2.1. Parsing String Input (response to post by syg96) | Reply
That's exactly why I prefer to use STL :-) Does it look better now?
Re: 2.1. Parsing String Input (response to post by Nickolas) | Reply
The added split code contains memory leak=)
delim and buf are duplicated strings - free should be called to deallocate them.

Though it will work well in SRM... But anyway it is bad style and at least comment should be added.

C programmers have to follow strict discipline for their low-level possibilities=)
Re: 2.1. Parsing String Input (response to post by syg96) | Reply
Agreed, added strtok-based implementation of split(). Thanks!
Re: 2.1. Parsing String Input (response to post by Nickolas) | Reply
How about C parsing functions? They are also useful. Personally I always use strtok instead of stringstream.

Example:
There is a string S which is a text. The separators are: space, period and comma. There may be several separators between words and separators before first and after last words.

char buff[1024];
strcpy(buff, S.c_str());
for (char *word = strtok(buff, " .,"); word; word = strtok(0, " .,"))
    ProcessWord(word);


Be careful with char[] size and remember that "buff" is changed by strtok.
The last: as with any C vs C++ library functions: strtok may be faster than stringstream.
2.1. Parsing String Input | Reply
RSS