If you need return few strings, use one of next: std::pair boost::tuple structure If you don't know how many strings your function will return - use something like std::vector.
If you need return few strings, use one of next: - std::pair - boost::tuple - structure If you don't know how many strings your function will return - use something like std::vector.
1 Soon, it will be std::tuple, not boost::tuple :-) – Zifre Jun 14 '09 at 13:34.
If it were me, I'd return a vector of strings. With an array, you'll need to worry about memory management. You could also pass the string in as a const reference, if you don't want to modify it.
I got it from the Top Coder practice sessions. It's been asked in the question to return an array of strings. – abc Jun 14 '09 at 12:57.
I'd say the options are: For a fixed number of strings: 1) return a pair, a tuple, or a struct that you define yourself. Something like: struct BrokenDownString { std::string firstbit; std::string middlebit; std::string endbit; }; Then either: BrokenDownString decode(std::string message); or just give BrokenDownString a constructor taking the message as a parameter. 2) Take multiple out params by pointer or non-constant reference: void decode(const std::string &message, std::string &out_1, std::string &out_2) { out_1 = /*whatever*/; out_2 = /*whatever*/; } For two strings, anything else (even an array) is overkill.
For a variable number of strings, however, you can either: 1) return a std::vector (be aware that this may result in excess copying). 2) take a std::vector & as a parameter, and append the results (this may copy strings but not the container). 3) make decode a function template, taking an output iterator as a parameter: template void decode(const std::string message, OUT out) { // do parsing *(out++) = firstbit; *(out++) = nextbit; // etc.} Then if the caller wants the results in a vector: std::vector v; decode(message, std::back_inserter(v)); If the caller prefers them in a deque: std::deque d; decode(message, std::back_inserter(d)); If the caller wants them in a list in reverse order: std::list l; decode(message, std::front_inserter(l)); And so on.
4) If you want something like the above, but for whatever reason you don't want to write template code, make decode take as a parameter an object, which it notifies of each string: struct DecodeTarget { virtual void append(const std::string &) = 0; }; void decode(std::string message, DecodeTarget &out) { // do parsing out. Append(firstbit); out. Append(nextbit); // etc. } Then if the caller wants the results in a vector: class VectorTarget : public DecodeTarget { private: std::vector &results; public: VectorTarget(std::vector &v) : results(v) { } void append(const std::string &bit) { v.
Push_back(bit); } }; std::vector v; VectorTarget vt(v); decode(message, vt).
If you're feeling hardcore and up for the challenge, you could return a char**. However, this is very error prone, so you should probably stick to a standard container or string class instead.
Read lysator.liu. Se/c/c-faq/c-2. Html#2-10 – siddhant3s Jun 14 '09 at 13:09 @siddhant3s: He means a char**, just like argv in main.
Use a NULL terminator, and this is the old fashioned c way of doing thing. You should treat this approach as deprecated in c++, but it will work just fine. – dmckee Jun 14 '09 at 14:45.
For your information, arrays are evil (siddhant3s.googlepages.com/how_to_tell_r... and parashift.com/c++-faq-lite/containers.ht...) Lately, if you still want to return array of std::strings, you can but only if you promise me that you will delete the returned pointer accordingly: #include #include std::string* F1() { std::string* s=new std::string2; s0=" return s; } int main() { std::string* ss=F1(); std::cout.
Best compile without exceptions, since otherwise you leak memory if anything throws... – Steve Jessop Jun 14 '09 at 13:25.
If you absolutely must return an array, you can use the fact that raw arrays decay into pointers. Here's some sample code that does what I think you want: #include #include #include std::string * decode() { std::string *ret = new std::string2; ret0 = "foo"; ret1 = "bar"; return ret; } int main() { std::string *baz = decode(); std::cout.
Pass the strings to the functions as "out" parameters - either by pointers or references and modify them within the function.
1) return a pair, a tuple, or a struct that you define yourself. Or just give BrokenDownString a constructor taking the message as a parameter. For two strings, anything else (even an array) is overkill.
1) return a std::vector (be aware that this may result in excess copying). 2) take a std::vector & as a parameter, and append the results (this may copy strings but not the container). And so on.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.