Jump to content
  • Advertisement
Sign in to follow this  
tom_mai78101

Std::string and iterator: Trying to get a second std::String out from iterator.

This topic is 2847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm scanning a string ending with an equal sign, if it contains parentheses, use a recursive function to do mathematics within that parenthese, then break out of the recursive function until the string iterator reaches the end.

The code:

bool Parenthese (std::string &ptr)
{
std::string::iterator left = ptr.begin();
while (left <= ptr.end() - 1)
{
if (*left == '(')
{
Parenthese ( /* THIS PLACE */ );
break;
}
left++;
}
std::string::iterator right = left;
while (*right != ')')
{
//Code snipped, as it doesn't matter with this problem.
}
//The rest of the code is merely pure mathematics.
}




The commented "THIS PLACE" is the place where I can't seem to use my iterator and convert it to string. What should I do? What else can I do in order for me to pass a std::string argument from an iterator? Do I have no other choice but to rewrite the whole code, and don't use std::string and its iterator? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
One option is to rewrite the code in terms of two iterators. This is more efficient as you don't need to make a copy of the substring during recursion. It is also easier to use some standard algorithms with the iterators.

typedef std::string::const_iterator StringIt;

bool Parentheses(StringIt begin, StringIt end)
{
// Example of using stuff from #include <algorithm>
StringIt left = std::find(begin, end, '(');
StringIt right = std::find(begin, end, ')');

// ...
}

// Keep an easy to use interface
bool Parentheses(const std::string &string)
{
return Parentheses(string.begin(), string.end());
}


Alternatively, you can keep your code using strings and build a substring by using the constructor that takes a pair of InputIterators. The iterator types of a std::string can be used as an InputIterator to this function:

bool Parenthese (std::string &ptr)
{
std::string::iterator left = ptr.begin();
while (left <= ptr.end() - 1)
{
if (*left == '(')
{
std::string string( /* left + 1 */, /* ptr.end() ? */);
Parenthese(string);
break;
}
left++;
}
std::string::iterator right = left;
while (*right != ')')
{
//Code snipped, as it doesn't matter with this problem.
}
//The rest of the code is merely pure mathematics.
}

Share this post


Link to post
Share on other sites
Thank you. I have implemented the code above, but I realized it wouldn't work out well enough if the math problem have nested parentheses.

Nonetheless, the main problem is solved. :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!