4 ms·
I donate you one: #include <vector> #include <string> #include <iostream> typedef std::vector<std::string> Strings_t; Strings_t split(const std
by dan00 16y ago
I donate you one:
#include <vector>
#include <string>
#include <iostream>
typedef std::vector<std::string> Strings_t;
Strings_t split(const std::string& string, char onChar)
{
Strings_t splitted;
size_t lastSplit = 0;
for (size_t i = 0; i < string.size(); ++i) {
if (string[i] == onChar) {
if (lastSplit != i)
splitted.push_back(std::string(string, lastSplit, i - lastSplit));
lastSplit = i + 1;
}
}
if (lastSplit != string.size())
splitted.push_back(std::string(string, lastSplit, string.size() - lastSplit));
return splitted;
}
int main()
{
Strings_t strs = split("$$qqqq$$$w$$$eee$$a$d$$$$$", '$');
for (Strings_t::const_iterator s = strs.begin(); s != strs.end(); ++s)
std::cout << *s << "\n";
return 0;
}
- d0m 16y agoI never said I didn't know where to get one, or to actually write it. In fact, you can do it way simply with getline(). It just always pissed me off when I hack a little something and there is no split.. I then need to either include boost which is huuuuge compared to my 200 lines file.. code it myself, get a snippet from internet. The point is: in C++, there are so much things.. It's like a fuckload of thousand of features to satisfy everyone. How the fuck can there be no split? Ruby, split, python, split, java, split, php, split, C# split, C++ -your-20-lines-function-which-only-support-splitting-on-a-char-but-not-on-a-string.
- dan00 16y agoWell, all these languages are very stringy because they are very weby.