Word lookup in string and left and right characters cut

Source: Internet
Author: User
Tags function prototype
The demand stems from the string processing that adds tag to the blog post: tag to "," separated, according to this separator separate each tag; In addition, in the habit of users may be typing extra space, so you need to tag the left and right space to cut. STL String Class There is no ready-made way to write one yourself. The first version of the function prototype is as follows:
  
   
   
void Find_all_words ( Const string & Char Decollator, Vector < string > words);

Where the parameter decollator represents a separator,words is used to store separate words (tag).


Given that the vector does not have a sort function, the caller may expect to store the word in the list, or it may choose a map or other type of container depending on the requirements. So the third parameter should be more flexible to consider generics but the function is still limited because only the STL standard container can be selected.

requirements are diverse, and perhaps users want to be stored as static arrays may want to print directly, then this variety of requirements can be satisfied? The answer is yes, and don't forget that there are two powerful weapon functions and lambda expressions in C + + 0x, so our second version of the function prototype is as follows:

  
   
   
void Find_all_words ( Const string & Char Decollator, function < void ( string & ) > push);


Its invocation looks like the following:

  
   
   
List < string > words;
' , ' , [ & words] ( string & s) {words.push_back ();});

In addition, we also need to cut out the word to do the cutting operation, the normal processing logic is: "Separate words-> cut processing-> store Words", of course, we can get the word list and then do the cutting process, But this logic is not very intuitive and will bring some efficiency overhead (at least go through the list again), so we again picked up the powerful weapon of C + + 0x to add a function type parameter to the word preprocessing. The call to the function becomes the following:

  
   
   
List < string > words;
= [ & words] ( string & s) {Words.push_back ();};
= [] ( string & ' ' ); };
' , ' , Push_word, Trim_word);

The function prototype has determined that the remaining algorithm problem is a piece of cake. The complete implementation of the function is as follows:

  
  
--> 1 void Find_all_words ( Const string & str, Char Decollator, Function < void ( string & ) > Push, Function < void ( string & ) > Pre = null)
2 {
3 string :: Size_type start_pos = 0 ;
4 Todo
5 {
6 string :: Size_type dec_pos = str.find_first_of (Decollator, Start_pos);
7 string Word = str.substr (Start_pos, Dec_pos - Start_pos);
8 if (Pre != null) pre (word);
9 if (Word.length () > 0 ) push (word);
Ten Start_pos = Dec_pos != string :: NPOs ? Dec_pos + 1 : Dec_pos;
One
of } while (Start_pos < str.length ());
of }


The two functions that cut the left and right characters of a string are simpler and are implemented as follows:

  
  
--> 1 void Trim_left ( string & S, Char c)
2 {
3 if (S.length () == 0 ) return ;
4 string :: Size_type pos = s.find_first_not_of (c);
5 S.erase ( 0 , POS);
6 }
7
8   void Trim_right ( string & S, Char c)
9 {
Ten if (S.empty ()) return ;
One string :: Size_type pos = s.find_last_not_of (c);
of POS == string :: NPOs ? s.clear (): S.erase (POS + 1 , S.length () - POS - 1 );
of }
-
-   void Trim ( string & S, Char c)
- {
- Trim_right (S, c);
A Trim_left (S, c);
- }
-  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.