string splitter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

string splitter

I have made a string splitter it can split string into vector of any numeric type. https://code.sololearn.com/cuz6gSCzO2co/?ref=app second approach :string is splitted to smaller strings like : "abc def ghi" => {"abc","def","ghi"} delim=' ' https://code.sololearn.com/cxr6SqIQOPGi/?ref=app but spliting into strings isn't possible using same algorithm as that for numeric types because if I read data to a string it'll read entire line without caring about delimeter and datatype.(read first code to understand what I mean) Both approaches are different. If I try to solve first problem using second algo I'll have to convert all strings to numbers. like "29" =>29 which isn't convenient as I need to work with multiple types (int, float, double) in same function and I'll not be able to use templates. I wish I could overload both functions but I'm unable to do that also because both have same parameters. I want more generalized way of splitting strings. It'll be great if first approach can work for std::string

28th Dec 2019, 9:06 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
5 Answers
+ 3
Sorry for the late reply, I had this thread bookmarked, but it was a busy day, so it took some time. The problem with getting the first version to work for std::string is that it would require an overload of the input operator >>, which would still need the delimiter being passed along, resulting in a different function call compared to the other basic types (+ it would need to be an external overload). Anyway, what you could do, is to specialize the template for strings, which is basically the same as an overload: https://code.sololearn.com/cZzbAmTj14wa/?ref=app Notice the missing default argument for "delim" in the explicit specialization; This is just because default arguments can only be declared once. Now, you still would end up with two different algorithms, but there would only be a single function call, which hopefully satisfies your requirements? Personally, I haven't been able to come up with a single algorithm, so if someone can provide a better solution, I would love to see it.
28th Dec 2019, 9:48 PM
Shadow
Shadow - avatar
+ 3
so basically you want to split a string into a vector containing the words? I'd do it like this: vector<string> strToVec(string data) { stringstream ss; ss.str(data); string temp = ""; vector<string> word_list; while (!ss.eof()) { ss >> temp; word_list.push_back(temp); } return word_list; } example: https://code.sololearn.com/cxG076RP5Ixs/?ref=app
28th Dec 2019, 12:28 PM
grdr
+ 2
Shadow , I didn't know that I can overload a function by its template parameters. As stated in question I wanted either to have single function that can work for all types of arguments or to overload them. Thanks a lot ^^
29th Dec 2019, 12:02 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 2
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰, yes, Template Specialization is quite a powerful tool. You can totally change a function's or classes' behaviour based on what type it is instantiated for. Using the <type_traits> library, you can also select a template instantiation based on certain characteristics of a type, which works thanks to the fact that substitution failures of the template parameters are not considered an error (as long as one fitting specialization is found that the compiler can actually use, that is), SNIFAE in short, in case you want to look that one up. So yeah, base lining, templates have become really powerful mechanisms in C++ (and are still being extended - oh yeah).
29th Dec 2019, 12:16 AM
Shadow
Shadow - avatar
+ 1
Gregor Dietrich , That's what I have already achieved in my first code. You can see me first code can work using delimiter. for example : split<int>("1,2,3,5,9,8,5") can give me an vector<int> Also it'll work for space seperated values. auto input4="This string has five words!"; auto words= split<std::string>(input4); It works! but if I try to split with delimiter : auto input4="This,string,has,five,words!"; words= split<std::string>(input4 , ',' ); this won't work. as first read from stream will extract all content into 1 single string. delimiter becomes useless. That's why I had to implement second code. But I have to do all splitting with first approach itself. I hope now it's clear what I was asking. your solution doesn't solve my problem, but thanks for trying to help ๐Ÿ™‚๐Ÿ™. I appreciate that.
28th Dec 2019, 1:14 PM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar