Another way of adding to a string (or output iterator)?

Discussion in 'C++' started by Joe Van Dyk, Apr 10, 2006.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    #include <string>
    #include <vector>
    #include <iostream>

    class Element
    {
    public:
    Element(std::string n) : name_(n) {};
    std::string serialize() const
    {
    return "<element>" + name_ + "</element>";
    }
    private:
    std::string name_;
    };

    int main()
    {
    std::vector<Element> container;
    container.push_back(Element("Joe"));
    container.push_back(Element("Bob"));

    std::string all_serialized;

    std::vector<Element>::const_iterator iter;

    // Is there a cooler way of doing this loop?
    // Like with std::back_inserter or std::copy or something?
    // Or perhaps create Element::eek:perator<< ?
    for (iter = container.begin(); iter != container.end(); ++iter)
    {
    all_serialized += iter->serialize();
    }

    std::cout << "All elements serialized: \n" << all_serialized
    << std::endl;

    return EXIT_SUCCESS;
    }
    Joe Van Dyk, Apr 10, 2006
    #1
    1. Advertising

  2. Joe Van Dyk

    Daniel T. Guest

    In article <>,
    Joe Van Dyk <> wrote:

    > #include <string>
    > #include <vector>
    > #include <iostream>
    >
    > class Element
    > {
    > public:
    > Element(std::string n) : name_(n) {};
    > std::string serialize() const
    > {
    > return "<element>" + name_ + "</element>";
    > }
    > private:
    > std::string name_;
    > };
    >
    > int main()
    > {
    > std::vector<Element> container;
    > container.push_back(Element("Joe"));
    > container.push_back(Element("Bob"));
    >
    > std::string all_serialized;
    >
    > std::vector<Element>::const_iterator iter;
    >
    > // Is there a cooler way of doing this loop?
    > // Like with std::back_inserter or std::copy or something?
    > // Or perhaps create Element::eek:perator<< ?
    > for (iter = container.begin(); iter != container.end(); ++iter)
    > {
    > all_serialized += iter->serialize();
    > }
    >
    > std::cout << "All elements serialized: \n" << all_serialized
    > << std::endl;
    >
    > return EXIT_SUCCESS;
    > }


    ostream& operator<<( ostream& os, const Element& elem ) {
    return os << elem.serialize();
    }

    // in main:

    stringstream ss;
    copy( container.begin(), container.end(),
    ostream_iterator<element>( ss ) );
    string all_serialized = ss.str();


    Of course, you could replace the stringstream with a file stream and
    *actually* serialize the elements...


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Apr 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,416
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    640
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,154
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,037
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page