Thread: stl for_each return type

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    stl for_each return type

    Hi,

    I was trying to write a function to concat all the elements in a vector<string> into single string. I was trying to use the stl for_each algorithm and using its return type as the concatenated string. I was told that for_each has the same return type as the function provided to it, so why does this not compile ? and how do I get it to work ?

    Code:
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<string>
    
    using namespace std;
    
    string concat(string& s);
    	
    int main()
    {
    	vector<string> u(10, "Hello");
    	string v;
    	v.reserve(1024);
    	v = for_each(u.begin(),u.end(),concat);
    	cout << v << endl;
    	return 0;
    }
    string concat(string& s)
    {
    	static string str;
    	str += s;
    	return str;
    }
    Thank you!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because it returns the pointer to the function concat, not a string.

    It seems to be an abuse of the for_each algorithm, seeing that there is a special-purpose accumulate in the <numeric> header.

    Code:
    v = std::accumulate(u.begin(),u.end(),std::string());
    For string manipulations you might also look into boost's string algorithms.

    Code:
    v = boost::join(u, "");
    Last edited by anon; 08-30-2009 at 03:57 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Ah, thank you! that seems like a much easier way to do it anyway.

    Because it returns the pointer to the function concat, not a string.
    Yes, I just found that out myself too, but could you give me an example of what you could use this return type for ?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, I just found that out myself too, but could you give me an example of what you could use this return type for ?
    You could obtain the result of concatenation from for_each, if you used a function object instead. For example, like this:

    Code:
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<string>
    
    using namespace std;
    
    struct concat
    {
        std::string result;
        void operator()(const std::string& s)
        {
            result += s;
        }
    };
    
    int main()
    {
    	vector<string> u(10, "Hello");
    	string v;
    	v.reserve(1024);
    	v = std::for_each(u.begin(), u.end(), concat()).result;
    	cout << v << endl;
    	return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Ahh, I tested it out and it worked, but I'm a little confused now.
    the function in the struct which is being called from for_each is the overloaded operator which has a return type of void. But in your example it is as if an object of the struct is being returned.
    Also, you are directly calling concat() without an object of the struct, and result is not static. So how does it retain its value ?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Indeed an instance of the struct is returned and then the result member is accessed to get the value. And the call of concat() creates an instance of the struct.

    A function object overloads operator(), so it can be used as if it was a function. But it is still an object that can have member variables.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Ahh, I just looked up it up. I believe this is the same thing as a functor ? But, it makes sense now, Thank you!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is a functor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM