Haikson

[ Everything is possible. Everything takes time. ]

Boost split - splitting a string

I've never had to write in C++. But then I decided to experiment. Task #1 is to write a program that will directly access postfix. I won't write why I need it. I think it's clear to everyone (spam is out of the question. just a large number of emails).

Since I have already implemented this in C++, I only needed to change the code a little and the program is ready. But the curse of all C programmers is working with strings. What can be done with a string in php or Java with a minimum of effort, the same thing in C requires dozens of lines of code, and sometimes the creation of entire class libraries :-)

This is where I came up with my task number 2 - to split the list of clients uploaded to a csv file into the FIRST NAME-LAST NAME-EMAIL fields. Having done a lot of manipulations with the code in a language that I already considered very complex, I was convinced that C++ would not give in to me so easily. I had to go for a trick.

After searching the Internet for the answer to my question only on page 4 of the search engine, I came up with a working example of splitting a string by a certain divisor. As it turned out, this functionality has been implemented in Boost for a long time.

The preface is enough... let's get right to the point. The function we need is described in the file

boost/algorithm/string/split.hpp
Template
template
      SequenceSequenceT &
      split(SequenceSequenceT &, RangeT &, PredicateT,
            token_compress_mode_type = token_compress_off);
And here is a ready-made working example:
#include <iostream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main(int argc, char**argv){
	typedef vector < string > splitted_vector_type;
	splitted_vector_type split_vec;
	string full_string;

	cin >> full_string;
	split(split_vec, full_string, is_any_of(";"));

	for (int i = 0; i < split_vec.size(); i++)
		cout << split_vec[i] << endl;

	system("PAUSE");
	return 0;
}
I hope this example will help many people save time.