Friday, May 20, 2005

split, join, strip in C++

Python에서 하던 일을 C++로 하기 위해서 만들었다.

 1 vector<string> mt_split(string str, string del)
2 {
3 int pos, old_pos=0;

4 string item;
5 vector<string> ret_vector;
6 while(1){
7 pos = str.find(del, old_pos);
8 if ( pos == string::npos ) {
9 item = str.substr(old_pos);
10 if(item.size()) ret_vector.push_back(item);
11 break;
12 }
13 item = str.substr(old_pos, pos-old_pos);
14 if (item.size()) ret_vector.push_back(item);
15 old_pos = pos+del.size();
16 }
17 return ret_vector;
18 }
19
20 string mt_join_inner(vector<string>::iterator b, vector<string>::iterator e, string j)
21 {
22 if (b != e-1 ){
23 return *b + j + mt_join_inner(b+1, e, j);
24 }
25 return *b;
26 }
27
28 string mt_join(vector<string> &v, string j)
29 {
30 return mt_join_inner(v.begin(), v.end(), j);
31 }
32
33
34
35 bool mt_isSpace(char ch)
36 {
37 if(ch == ' ' ch == '\t' ch == '\n' ch == '\r')
38 return true;
39 else return false;
40 }
41
42 string mt_remove_dup_space(string& instr)
43 {
44 if ( instr.size() < 2 ) return instr;
45 if ( mt_isSpace(instr[0]) && mt_isSpace(instr[1]) ){
46 return mt_remove_dup_space(instr.substr(1));
47 }
48 else return instr[0] + mt_remove_dup_space(instr.substr(1));
49 }
50
51 string mt_strip(string& instr)
52 {
53 // string outstring;
54
55 if(instr.size()==0) return "";
56
57 string::iterator fi;
58 for(fi=instr.begin();fi!=instr.end();fi++){
59 if (!mt_isSpace(*fi)) break;
60 }
61
62 string::iterator ri;
63 for(ri=instr.end()-1; ri!=instr.begin(); ri--){
64 if (!mt_isSpace(*ri)) break;
65 }
66
67 string outstring(fi, ri+1);
68 return outstring;
69 }

4 comments:

Anonymous said...

how to pod cast music
Information how to pod cast music

Stephen said...

Hey, Nice Blog!

Makes some very interesting reading.

Please be sure to check out my site here - Mininova - for some great bit torrent downloads.

Thanks

Katrina said...

Hey,

Very nice blog and some interesting posts. Have a look at my new bit torrent site called - Mininova -

Thanks

Handbag Express said...

Hello, just browsing around and thought I would peek at your blog also thought it would be a great opportunity to highly recommend both of these sites with spectacular deals. Grand Openings at **www.handbagexpress.com** for the ultmate handbag selections, and don't miss out on **www.VegasTours.com**for Spectacular Grand Canyon Vacation Tours, you will agree the offers are Super incredible after visiting. Thanks and have a great day.