split string

Jun 27, 2012 at 10:21am
I have created this basic examples:

#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>

string s = "a,b, c ,,e,f,";
vector <string> fields;
boost::split( fields, s, "," );
print( fields );

This is given me this error:
Description Resource Path Location Type
‘__pred’ cannot be used as a function teste3 line 222, external location: /usr/include/c++/4.4/bits/stl_algo.h C/C++ Problem

Can someone advise me of what I'm doing wrong?


I have also tried this second example:

std::string str1("hello abc-*-ABC-*-aBc goodbye");
typedef std::vector< std::string > split_vector_type;
split_vector_type SplitVec; // #2: Search for tokens
boost::split( SplitVec, str1, boost::is_any_of("-*") );

And this give me the error:
Description Resource Path Location Type
Invalid arguments '
Candidates are:#0 & split(#0 &, #1 &, #2, enum boost::algorithm::token_compress_mode_type)
' Switch.cc /teste3 line 82 Semantic Error


Can someone advise me of what I'm doing wrong in this two examples.

Thanks in advanced,
Regards,
CMarco
Jun 27, 2012 at 10:36am
Where is main()?
Jun 27, 2012 at 11:04am
Hello,

sorry, of course all the code is inside of the main().

I'm using eclipse and of course the boost split of boost C++ libraries.

This may sound crazy but can this be a bug of eclipse?
Because all the errors are in the boost::split :

boost::split( fields, s, "," ); // in the first example

and

boost::split( SplitVec, str1, boost::is_any_of("-*") );// in the second example

Regards,
CMarco
Jun 27, 2012 at 11:26am
What compiler, compiler version, boost version are you using?

Maybe your should try boost::algorithm::split(...); ?
See example on 172 line: https://github.com/sidorovis/system_utilities/blob/master/sources/property_reader/property_reader.cpp
Jun 27, 2012 at 12:11pm
Hello,

it works!!

I tested like this:

std::string id_equip="8";
std:string src="8+1729230-6+289191-7+1293190";

typedef std::vector< std::string > split_vector_type;
split_vector_type SplitVec;

boost::algorithm::split(SplitVec, src, boost::algorithm::is_any_of(id_equip) )

EV << "buffer: " << SplitVec.size() << "\n";

In the end the SplitVec.size() will have to have the number 2 because it finds the number 8 two times in the src string right? I'm not sure.

Regards,
CMarco
Jun 27, 2012 at 2:16pm
In the end the SplitVec.size() will have to have the number 2 because it finds the number 8 two times in the src string right? I'm not sure.

If you're not sure, compile and run: http://ideone.com/PfySZ
Jun 27, 2012 at 3:09pm
Hello,

Firts of all tahnks for the tip.

I think split will not do what I want, because, I want to see if the number 8 in string src repeat it self more than 2 times an if so I have to return a 0; I'm thinking in regex...
Can someone advise me?

Regards,
CMarco
Jun 27, 2012 at 3:18pm
Hi,

I'm trying to test this:

std::string id_equip="8";
std:string src="8+1729230-6+289191-7+1293190";

typedef std::vector< std::string > split_vector_type;
split_vector_type SplitVec;

boost::algorithm::split_regex(SplitVec, src, boost::regex("8"));
but it gives me error
undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'

I also test with:
boost::algorithm::split_regex(SplitVec, src, boost::algorithm::regex("8"));
But it gives me erro:
‘regex’ is not a member of ‘boost::algorithm’

I load the other libraries:
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/regex.hpp>

Can someone give me a tip

Regards,
CMarco
Jun 27, 2012 at 3:43pm
I want to see if the number 8 in string src repeat it self more than 2 times an if so I have to return a 0; I'm thinking in regex

In that case you don't need to split the string, and you don't need a regular expression either. Either you're checking if std::count(str.begin(), str.end(), '8') > 2 or you're checking if str.find("888") != std::string::npos, depending on whether it's any 2+ numbers 8 or 2+ adjacent 8s.
Last edited on Jun 27, 2012 at 3:43pm
Jun 27, 2012 at 4:10pm
Hello,

Ok thanks, but I think that will not work for me because I have to put the id_equip that is a string instead of the '8' because the id_equip will have a get method that will collect random numbers.

If you have some other tip I will be glade to know.

Regards,
CMarco
Jun 27, 2012 at 5:12pm
Hello,

To be more precise I have two strings like:

std::string src = ttmsg->getSource(); // the string src have a lot of numbers like +8-23+7-5+67-123+90-321+8- and so on... is big, the signal + and - separate the ID that I want from the others that I do not want.

and I have other string with the module Id:
std::string teste;
std::stringstream teste_aux;

teste_aux << getId();
teste = teste_aux.str();// the string teste have the Id of my module that is also a number.

id_teste = "+" + teste + "-"; // to the integer I add the signal + and -

How can I search in string src if the Id of my module, that i have put between signal + and - and that is in string id_teste, repeats more than two times in string src.

Can someone please advise me?

Sorry for not being so detailed at first but I was thinking at first that the split function will be my solution.

Best Reagrds
Marco
Jun 28, 2012 at 8:08am
Hi,

can someone advise me in what will be the best method, for to the search.

Regards,
Marco
Jun 28, 2012 at 3:05pm
Hi,

I was able to solve my question using str.find and src.substr(found+3).

Example:
found = src.find(id_equip);
if (found!=string::npos){
string str1 = src.substr(found+3);f
found = str1.find(id_equip);

Thanks Ivan Sidarau and Cubbi for the tips...

Regards,
CMarco
Topic archived. No new replies allowed.