Clear iterator


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Clear iterator

  1. #1
    Join Date
    May 2004
    Posts
    242

    Clear iterator

    Hello,

    I have a method which splits a string based on @ sign. I am only interested in keeping the details of the string with the @ in it. An example of the passed string is is: "Hello@World today is sunny".
    Thus, as a result I'd like to push into my vector<string> only "Hello" and "World" and ignore everything else.

    I have :-

    bool isat(char c)
    {
    if (c == '@')
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    and :-

    vector<string> my_split(const string& str)
    {
    ...
    typedef string::iterator iterAt;
    vector<string> ret;
    ...
    iterAt atLoc = std::find_if(string(i,j).begin(), string(i,j).end(), isat);
    cout << "\tatLoc = [" << *atLoc << "]\n";
    if (isat(*atLoc))
    {
    ret.push_back(string(i,j));
    }
    ...
    }
    Don't worry about the i,j - those are iterators for ignoring leading blanks and find end of next word and they are calculated correctly.

    For some reason as I iterate over my string, atLoc does not clear itself. I thought that as it gets declared every time, it will be ok, but I'm getting tatLoc = @ every iteration. This results in my vector<string> consisting of the string I passed in but in the vector<string>.

    I tried doing *atLoc = '\0'; after the push_back, but that didn't help.

    Can someone please help ?
    Last edited by ami; 08-23-2007 at 06:41 AM.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    how about something like this? doesn't use an iterator but you can pass the '@' as "c":

    Code:
    void splitLine( const string& s, char c, vector<string>& v )
    {
       int i = 0;
       int j = s.find(c);
    
       while (j >= 0)
       {
          v.push_back(s.substr(i, j-i));
          i = ++j;
          j = s.find(c, j);
    
          if (j < 0) 
          {
             v.push_back(s.substr(i, s.length( )));
          }
       }
        
    }
    Last edited by nspils; 08-24-2007 at 12:10 AM.

  3. #3
    Join Date
    May 2007
    Posts
    843

    Thumbs up

    for (int loop=0;loop<vec.size();++loop)
    {
    if (string.at(loop) == '@")
    {
    vec.ignore()
    }
    else
    {
    vec.push_back(string)
    }
    }

    I don't know whether my code is working or not.

    Please try it and leave the comments here.

    Thanks.

    I hope this help.

Similar Threads

  1. clear screen console application vs 2005
    By wartech in forum .NET
    Replies: 1
    Last Post: 10-02-2006, 01:22 PM
  2. Replies: 1
    Last Post: 01-12-2006, 07:53 PM
  3. Iterator versus indexing
    By javadeveloper in forum Java
    Replies: 1
    Last Post: 11-11-2005, 10:25 AM
  4. Submit, Clear, and Cancel buttons on an ASP.NET form
    By Aldo Sarmiento in forum ASP.NET
    Replies: 0
    Last Post: 03-01-2002, 02:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center