Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Another way of adding to a string (or output iterator)?

Reply
Thread Tools

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

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      04-10-2006
#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: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;
}
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      04-11-2006
In article <(E-Mail Removed)>,
Joe Van Dyk <(E-Mail Removed)> 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: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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
page output contains output from another request jmr ASP .Net 2 04-26-2007 06:21 PM
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
Help: what is the quickest way to find out whether a string contains another string? tuweiwen@gmail.com Java 17 12-06-2005 01:04 PM
Is this the right way to redirect output from a class to another? Jason C++ 3 08-25-2004 01:12 AM
Counting occurances of string A in string B, and adding it to string B Sandman Perl Misc 7 08-03-2004 08:46 PM



Advertisments