-
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 05:41 AM.
-
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-23-2007 at 11:10 PM.
-
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
-
Replies: 1
Last Post: 10-02-2006, 12:22 PM
-
Replies: 1
Last Post: 01-12-2006, 06:53 PM
-
By javadeveloper in forum Java
Replies: 1
Last Post: 11-11-2005, 09:25 AM
-
By Aldo Sarmiento in forum ASP.NET
Replies: 0
Last Post: 03-01-2002, 01: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks