boost split

Hello to all,

I have a vector name vect with content:
1
2
vect[0] = 899-12312
vect[1] = 2617-12344


Is possible using split to split in the signal - an put then the result in a new vector called vect_aux?

I think on this:
1
2
3
for (size_t c = 0; vect.size(); ++c){
boost::algorithm::split(vect_aux, vect[c], boost::algorithm::is_any_of("-"));
}


This do not give me a build/compile error but when running the code the program crash.

Can someone advise me.

Regards,
CMarco
Last edited on
Replace "vect.size();" with "c < vect.size();" in the loop condition.

Also note that each call to split erases and re-builds the output vector.
Hi,

i think you go outside of the vector definition, try this :
1
2
3
for (size_t c = 0; c < vect.size(); c++){
boost::algorithm::split(vect_aux, vect[c], boost::algorithm::is_any_of("-"));
}


Good luck !
Hi,
1
2
3
std::vector<std::string> vect_res;
std::vector<std::string> vect_auxi; 
std::vector<std::string> vect;


It works if I do:

1
2
3
4
5
6
7
for (int c = 0; c < vect_aux.size(); c++){
      boost::algorithm::split(vect_aux, vect[c], boost::algorithm::is_any_of("+"));
/* instead of size_t i have to use int and instead o ++c I have to use c++. If like this all goes ok.*/
}

EV << "vect_aux: " << vect_aux[0] << '\n'; // it shows: 899 like i want.
vect_res[2] = vect_aux[0];// this equal is given me an error when running. is something wrong with this? 


Regards,
CMarco
Last edited on
Hi,

In
vect_res[2] = vect_aux[0];
is given me exit 139 error - is only this line.

someone knows what this means?

Regards
Last edited on
Hi,

thanks to all for the tips.
for this last error the solution that i think I find:

http://www.cplusplus.com/reference/stl/vector/insert/

[code]
std::vector<std::string>::iterator it;
it = vect_res.begin();
EV << "vect_aux: " << vect_aux[0] << '\n';

vect_res.insert(it, 2, vect_aux[0]);


Regards,
CMarco
Last edited on
Hi,

sorry but, no go yet.

1
2
3
4
5
6
7
8
9
std::vector<std::string>::iterator it;
it = vect_res.begin();
EV << "vect_aux: " << vect_aux[0] << '\n';

vect_res.insert(it, vect_aux[0]); // it goes OK

vect_res.insert(it+1, vect_aux[1]); // do not work
     // or
vect_res.insert(it, 1,vect_aux[1]); // do not work 


Can someone advise me how can I put in position 1 of vector vect_res the content of vector vect_aux[1].

Regards,
CMarco
Hello,

Someone have an advise regarding this.

Regards
inserting in a vector may invalidate the iterator you got previously.

This would work:
1
2
3
vect_res.insert(vect_res.begin(), vect_aux[0]);

vect_res.insert(vect_res.begin(), 1,vect_aux[1]);


Or you try something like this:

1
2
3
4
vect_res.resize(2);

vect_res[0] = vect_aux[0];
vect_res[1] = vect_aux[1];
Hi,

Thanks again.
Yes you here right inserting in a vector invalidate the iterator I got previously.

Is better like this:

1
2
3
4
5
6
7
vect_res.insert(vect_res.begin(), vect_aux[0]);
vect_res.insert(vect_res.begin(), 1, vect_aux[1]);

//int a; is a number in a for cycle:
vect_ress.insert(vect_res.begin(), a*2, vect_aux[1]);
vect_res.insert(vect_res.begin(), a*2+1, vect_aux[0]);


Regards
Topic archived. No new replies allowed.