String C++ question

Since I have been using Java so long and not C++ I don’t remember any of it. So I want to use:

newProduction = newProduction.replaceAll(“F”, rule_);

–working on a L-System

What is the C++ equivalent of that?

try this (stolen from here: http://bytes.com/forum/thread139356.html)

  
  
#include <string>  
#include <algorithm>  
  
using namespace std;  
  
...  
  
string str = "..F...F....";  
replace(str.begin(), str.end(), 'F', _rule );  
  

hth
d

I got an error:
no matching function for call to 'replace(…etc…

also what is the .charAt(); equivalent?

As far as I can tell that version of replace is not working in OF. I need to iteratively replace a string within itself. Lets say I have string F++F-F–FF I need it to to become F++F-F–FF++F++F-F–FF-F++F-F–FF–F++F-F–FFF++F-F–FF. That could be off but you get the idea. I guess I could use a for loop and every time I come across an F just replace that with rule_. Maybe I am missing something.

Can OF use regular expressions like regex? That would make this easier.

[quote author=“AMCohen”]I got an error:
no matching function for call to 'replace(…etc…

also what is the .charAt(); equivalent?[/quote]

str.charAt(5) is in C++:
str[5]

best
joerg

no, there is no regex built into C++.

i think you’re going to have to do this by hand, sorry… look through the STL String reference at http://www.sgi.com/tech/stl/basic-string.html.

in the 0.06 release (which is in being actively finished now) we include poco, which includes regex functionality:

http://www.appinf.com/docs/poco/Poco.Re-…-ssion.html

there are also some regex libraries around, some of them are quite big (and require boost) but others look manageable, like this one:

http://www.geocities.com/siliconvalley/-…-ubdom.html

Do you need regular expressions in your C++ program? And you don’t want neither to use some “big” library having regular expression as its part nor to port some existing C library to C++ nor (of course) to write it by yourself? I

hope that helps!!

take care,
zach

Thanks for all the help. I’ll check out all these links. I would use regular expressions because I am having major memory issues with the strings. Being iterative these get very large quickly. There are a couple of other methods I am going to try I’ll post them when they are working.

This is messy but it works! prod_ is being passed from another function to this function

string newProduction = prod_;

int posArray[50];
int counter = 0;
int strLgth = newProduction.length();
for (int i = 0; i < strLgth; i++){
if (newProduction[i] == ‘F’){
posArray[counter]=i+1;
counter++;
}
}

boolean_p gate = false;
int z = 0;
while (gate == false) {
newProduction.insert(posArray[z], rule_, 1, rule_.length());
z++;
posArray[z] = posArray[z]+z*(rule_.length()-1);
//printf("%i\n",posArray[z+1]);
if (z == counter) {
gate = true;
}
}