string iterator

This is a discussion on string iterator within the C++ Programming forums, part of the General Programming Boards category; I'm reading one tutorial about STL iterators. I've noticed that there is iterator support for strings. For example I can ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    string iterator

    I'm reading one tutorial about STL iterators. I've noticed that there is iterator support for strings. For example I can do something lke this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s("Kivla");
        string::iterator it;
       
       for (it = s.begin(); it != s.end();++it)
       {
           cout<<*it;
       }
    }
    I wonder what is the benefit of using iterators with strings (there must be some benefit otherwise iterators wouldn't have been defined)?
    In which cases they are useful for strings?

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    They are usefull in all the situations where you want to use the string object with the STL.

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
        string s("Kivla");
    
        cout << "Before toupper: " << s << endl;
        transform(s.begin(),s.end(),s.begin(),toupper);
        cout << "After toupper : " << s << endl;
        sort(s.begin(),s.end());
        cout << "After sort    : " << s << endl;
    
        return 0;
    }
    Output:
    Code:
    Before toupper: Kivla
    After toupper : KIVLA
    After sort    : AIKLV
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This is a contrived multi-example, but it's not a big step to a potential real world use:
    Code:
    #include <deque>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string s ( "is a test" );
      string t ( "This " );
      deque<char> string_buffer ( s.begin(), s.end() );
    
      for ( string::reverse_iterator it = t.rbegin(); it != t.rend(); ++it )
        string_buffer.push_front ( *it );
    
      s = string ( string_buffer.begin(), string_buffer.end() );
      cout<< s <<endl;
    }
    My best code is written with the delete key.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Hmm, interesting, I'll dive more into STL... I almost sure I'll have questions regarding STL very soon. I hope I can count on your help.
    Thanks hk_mp5kpdw and thanks Prelude
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    well if you think of strings as just another container then:
    1: You have consistency with the other container
    2: Many(most) of the std::algorithm functions use iterators as others have shown by example

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Well, I'm pretty new to STL, I was playing with string class and container vector from time to time, but never actually scratch under the surface. Of coures that is going to change very soon...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21