c++ - Selectively populated vectors with substrings extracted from a source string -
i have char array, in contents following:
char buffer[] = "i1 i2 v1 v2 i3 v3 i4 v4";
as may see, it's typical blank separated character string. want put sub-string(s) starting letter "i" vector (ivector
), , sort elements in ascending order. @ same time, i'd want put sub-string(s) starting letter "v" vector (vvector
), , sort elements in ascending order. other(s) (e.g. "do" in example) ignored.
i'm not familiar stl algorithm library. there functions me achieve avove-mentioned job?
thank you!
you can iterate on substrings using std::istream_iterator<std::string>
:
std::stringstream s(buffer); std::istream_iterator<std::string> begin(s); std::istream_iterator<std::string> end; for( ; begin != end; ++begin) { switch((*begin)[0]) { // switch on first character // insert appropriate vector here } }
then can use std::sort
sort vectors, @billy has pointed out. consider using std::set
, keep items sorted in first place.
Comments
Post a Comment