473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

#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::operator<< ?
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;
}
Apr 10 '06 #1
1 2420
In article <Ix********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> 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::operator<< ?
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.
Apr 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1833
by: Christopher Richards | last post by:
I have a variable that stores a temporary password. I'm just learning about arrays. I know this is wrong $pwList= ($newPW); I want to add the value of the $newPW which keeps changing to a unique...
5
3238
by: Anders | last post by:
Hello there. Can anyone tell me why the ++-operator is defined for ostream_iterator ? I can't see the difference between: ostream_iterator<int> test(cout); test++ = 1; test++ = 2; test++ =...
3
3121
by: silverburgh.meryl | last post by:
i have the following code, but it does not compile because I pass in back_inserter(b) as the output iterator in the remove_copy_if algorithm. My questions are: 1. why it does not compile? Passing...
1
2404
by: Allerdyce.John | last post by:
Can I use back_inserter as the output iterator? The following code wont't compile, is there a reason for that? How can I achieve what I want? (grow aList if needed) class build { public: A*...
8
3665
by: Steve Edwards | last post by:
Hi, 1) I'm making a container for string pointers... set<string*> strSet; strSet.insert(...); // insert some strings ....
5
3058
by: KL | last post by:
I have a problem trying to write to a binary file that I want to name after a particular name from a set. My code for the function follows, please excuse the horrible mistakes you may see, I am a...
1
3751
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
2
1234
by: rocky1821 | last post by:
Good day all, Is there another way to output? For example, if I am trying to output something, do I always have to put it in this form? cout<< "output" <<endl; Are there any other ways?
5
12081
by: bodycode | last post by:
I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level. I want...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.