Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL question

Started by gumpy Apr 5, 2005 at 8:00 AM 4 replies 1.1k views
Original Post
gumpy
gumpy
i'm hoping this is the proper place for this thread. after seeing boolean's thread the other day on "worst hello world" i decided to use that idea to become more familiar with stl. in this example, i was looking for a solution to using for_each without having to declare the data type of the contents of the container. the only thing i could get to work is this structure.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct f{
    template<typename T>
    void operator()(T&t){std::cout<<static_cast<char>(t);}
};
int main() {
    std::string s("Hello World!");
    std::vector<int>v(s.begin(),s.end());
    for_each(v.begin(),v.end(),f());
    return 0;
}


i was wondering if this is only or proper way to do this.
This space for rent.
Telastyn
Telastyn
#include <string>#include <iostream>#include <vector>#include <algorithm>using namespace std;class   printfun{private:public:        template <typename T>        void    operator()(T t){                cout << t;        }};int main(int argc, char *argz[]){string  foo="foo: Hello world.";char    bar[]="bar: Hello world.";vector<char>    moo;moo.push_back('m');moo.push_back('o');moo.push_back('o');for_each(foo.begin(),foo.end(),printfun());cout << endl;for_each(&bar[0],&bar[strlen(bar)],printfun());cout << endl;for_each(moo.begin(),moo.end(),printfun());cout << endl;}


produces:

foo: Hello world.bar: Hello world.moo


Though mine doesn't pass by reference, simply because I'd forgotten until now...
Fruny
Fruny
#include <iostream>#include <string>#include <vector>#include <algorithm>#include <iterator>int main(int argc, char *argz[]){  std::string str = "Hello World.";  std::copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout, ""));  std::cout << std::endl;  std::vector<char> vec(str.begin(), str.end());  std::copy(vec.begin(), vec.end(), std::ostream_iterator<char>(std::cout, ""));  std::cout << std::endl;}


If you don't want to declare the type of the contents of the container, your solution is acceptable. Though I wouldn't bother with the static_cast - if you have a std::vector, you're going to write ints, right? If not, well, you'll have to specify the type you want to print out somehow, which brings us back into the domain covered by std::ostream_iterator. [smile]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
gumpy
gumpy
well ya, it's a pointless example. the concept i pointed out is what i'm looking at. and ya, it works as a class as well as a struct.
This space for rent.
Fruny
Fruny
Quote:
Original post by gumpy macdrunken
well ya, it's a pointless example. the concept i pointed out is what i'm looking at. and ya, it works as a class as well as a struct.


Ok, then consider something like this, which might be what you were looking for:

template<class T> struct Foo{  T m_t;  Foo(T t) : m_t(t) {}};template<class T> Foo<T> make_foo(T t){   return Foo<T>(t);}


The type T is deduced from the parameter you passed to make_foo. If you pass it an int, you get a Foo, if you pass it a std::string, you get a Foo. If you're using such a make_foo as a parameter to a function template (e.g. for_each or copy), you never need to explicitely refer to Foo<whatever>.

That's how std::make_pair, std::bind1st, std::bind2nd, std::mem_fun, std::mem_fun_ref, std::ptr_fun ... work.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Jingo
Jingo
Prefer std::ostreambuf_iterator :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.