can i do that in C++?
non of my books say anything about it.
thanks
Printable View
can i do that in C++?
non of my books say anything about it.
thanks
What for? Usually it's better to pass a pointer
well, i want to write a function similar to the "Split()" function in VB.
I made one pretty quickly using a vector (from the STL), although I could probably rewrite it a bit better.
Look up vector, copy, and iterators in the reference. You should find something. Also I posted code for my original non-optimised Split function which I'm sure you can speed up.
well parksie, can you pick any iterator_type as well as value_type? 2 input and 4 output iterators and match value would do
I think you can mix&match most things between them as long as they have the same conceptual type (for example you just need to pass an iterator, so any kind of iterator will work).
well i found this
http://forums.vb-world.net/showthrea...ht=vector+copy
but it's not templatized :p
Aha you noticed ;) It was before I particularly cared about things like that :p
I think the STL docs have an example of this, and once my poor brain gets its act together I can knock something useful up I think.
Of course, I notice everything ;)
to be as generalized as possible I'd have two functions, one in style of instr, returning a outputiterator of 2 inputiterators, then have one that copies over the intial part to a container/array in conjunction with instr, then use that iteratively to fill a container/array
uhh im lost.
im putting them into a header file. so far i have left, right, replace(i dont think it works too well though), instr. i havent done vectors yet, but i guess ill have to now..
thanks
Before you completely lose it, VC++ does support simpler things.
STL vector templates are fun.
But, vanilla C does support returning arrays. You do it everyday.
PHP Code:char * myfunc(char *whatever){
char *tmp;
..........
return *tmp ;
}
This returns an array of char, using a pointer
long *myfunc(long *stuff );
{
return &long[0];
}
This returns an array of longs., And so on.
oh
soo if i do
Code:
//main:
char returnstuff[];
returnstuff=stuff("hello");
char *stuff(char *blah){
char otherstuff[]=blah;
return *otherstuff;
}
//crappy example but that would return an array?
but in jims example thats what it shows, i think
thanks for the help btw
if you allocate the array on the heap it won't run out of scope, but on the other hand it leaves the possibility the client could forget to delete it, so from a OOP perspective returning an array on the heap is bad programming practice. You should pass an array instead, and then if you want, return a reference to it
jims example has a typo:
it should readCode:char * myfunc(char *whatever){
char *tmp;
..........
return *tmp ;
}
return tmp;
and one can only hope the string in tmp does not run out of scope
in nabeels code is the problem that
char returnstuff[];
has a range of 0, therefore is not able to save anything.
My recommentdation is to pass a pointer to a pointer that will receive the starting address of the array. Or pass a pointer that will be filled (has to have memory associated before).
You could also implement the split function using a callback function, but that is complicated.
I'd use this prototype:
I think count and compare are not really needed.Code:void Split(char* str, char** arStrings, int iMaxStrings, char* delimiter = " ");
What you have to do is allocate an array of strings and call the function. iMaxStrings is the size of the array. In the function, use strcpy to fill the array.
alright, im trying that.
thanks