C ++ engineers to develop a daily question 4.5 (iterator traversal)

First, on behalf of this problem when a problem is not an integer daily numbers are relatively simple, I organized here again mainly because I did wrong (yes I was too dishes you like big brothers to ignore)

topic:

In a string str, output string str longest consecutive string of digits

 

 

This question clearly thought my own solution given below:

Using an iterator my first time to do the right answer to traverse completed

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 void FFind(string s){
 5     auto it = s.begin();
 6     string a,res;
 7     while (it != s.end()){
 8         if (*it >= 48 && *it <= 57){
 9             a =a + *it;
10         }
11         else{
12             if (a.size() > res.size()){
13                 = RES A;
 14                  a.clear ();
 15              }
 16              the else {
 . 17                  a.clear ();
 18 is              }
 . 19          }
 20 is          IT ++ ;
 21 is      }
 22 is      IF (a.size ()> res.size ()) {          // because fail to end () will cause the program to crash position, so iterators to traverse the last word when a position outside the loop must be determined once this condition, unsightly and reduces the readability of the program 
23 is          RES = a;
 24      }
 25      COUT << RES;
 26 is  }
 27  int main () {
 28     string s;
29     getline(cin, s);
30     FFind(s);
31     return 0;
32 }

After I finished decisively to find a program written by other chiefs, only to find this person I think the problem is too old trouble! !

Always forget the string can also use [] to take the contents, so it will not cause traversal can not determine the last cycle (next position last position)

Gangster answer is as follows:

 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 int main()
 5 {
 6     string s, res, cur;
 7     cin >> s;
 8     for (int i = 0; i <= s.length(); i++)
 9     {
10                                                        
11         if (s[i] >= '0' && s[i] <= '9')                 //使用 [ ] 符来取内容 
12         {
13 is              CUR + = S [I];                                
 14          }
 15          the else 
16          {
 . 17                                                             // find longer strings, the string is updated 
18 is              IF (res.size () < cur.size ())
 . 19                  RES = CUR;
 20 is              the else 
21 is                  cur.clear ();
 22 is          }
 23 is      }
 24      COUT << RES;
 25      return  0 ;
 26 is }

~ Sale spinach, spinach! Sale! ! spinach! ! Sale! ! ! spinach' ! ! ! !

Guess you like

Origin www.cnblogs.com/Kaniso-Vok/p/11832501.html