Author Topic: function as 4th parameter in replace_if.  (Read 3552 times)

Gametaku

  • .
  • Founders
  • Posts: 168
  • Karma: +13/-4
  • .
function as 4th parameter in replace_if.
« on: January 27, 2007, 02:31:54 PM »
is it possible to use a function that returns, the proper type as the 4th perameter

for example instead of



replace_if ( v.begin(), v.end,  hasCond, "use this string")

to use:

replace_if(v.begin, v.end(), hasCond, function_That_Returns_string)

(where v is a sting vector)

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: function as 4th parameter in replace_if.
« Reply #1 on: January 27, 2007, 02:58:44 PM »
Well you could do it with say

Code: [Select]
replace_if(v.begin(), v.end(), hasCond, ReplacementFuction());
but I don't think you could do
Code: [Select]
replace_if(v.begin(), v.end(), hasCond, ReplacementFuction);

unless of course you were dealing with a vector of function pointers.

Gametaku

  • .
  • Founders
  • Posts: 168
  • Karma: +13/-4
  • .
Re: function as 4th parameter in replace_if.
« Reply #2 on: January 27, 2007, 03:09:26 PM »
well, that works I guess I didn't think of using the paranthasis because the hasCond function leaves them off.

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: function as 4th parameter in replace_if.
« Reply #3 on: January 28, 2007, 10:13:45 AM »
there's a major difference between the two...and if you can't see it, you have some more to learn

Gametaku

  • .
  • Founders
  • Posts: 168
  • Karma: +13/-4
  • .
it's broken.
« Reply #4 on: January 28, 2007, 10:32:40 PM »
or needed sleep.

So here is another stupid question:

getline is suppose to  read characters unti:

num - 1 characters have been read,
a newline is encountered,
an EOF is encountered,
or, optionally, until the character delim is read. The delim character is not put into buffer.

so why  is the following:

Code: [Select]
void
readRep (const string & in, vector <string> & v)
{
  string temp;
  ifstream ins(in.c_str());

while (! ins.eof())
   {
   getline (ins, temp, '#' );
     v.push_back(temp);
   }
 
 ins.close();
}


void write (const vector <string> & v)
{
    for (unsigned int x = 0; x < v.size(); x++)
cout <<"v["<< x << "]: "<< v[x] << endl;
}

int main ()
{

  readRep ("replace.txt", replace);
  write (replace);
return 0;
}


displaying:
v[0]: Mark
v[1]: CSCI362
Dave
v[2]: CSCI420

and not:
v[0]: Mark
v[1]: CSCI362
v[2]: Dave
v[3]: CSCI420

where replace.txt contains:

Mark#CSCI362
Dave#CSCI420




Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: function as 4th parameter in replace_if.
« Reply #5 on: January 28, 2007, 10:54:34 PM »
because
Quote
num - 1 characters have been read,
a newline is encountered,
an EOF is encountered,
or, optionally, until the character delim is read. The delim character is not put into buffer.
is not correct.

It is
num - 1 characters have been read,
the delimiter character is read, which defaults to a newline.
an EOF is encountered,
The delim character is not put into buffer.

Gametaku

  • .
  • Founders
  • Posts: 168
  • Karma: +13/-4
  • .
Re: function as 4th parameter in replace_if.
« Reply #6 on: January 28, 2007, 11:07:06 PM »
thanks,

so is there anyway to have more then one delimiter character? (replace.txt can't be modified)

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
Re: function as 4th parameter in replace_if.
« Reply #7 on: January 29, 2007, 12:30:01 PM »
Get the line with the newline delim, then use a string tokenizer to break up the line into tokens based on the "#".

Gametaku

  • .
  • Founders
  • Posts: 168
  • Karma: +13/-4
  • .
Re: function as 4th parameter in replace_if.
« Reply #8 on: January 29, 2007, 03:46:18 PM »
yea, that's what I ended up doing.