Printing vector<string> with spaces between items.

M

Minkoo Seo

Hi list.

I'd like to print a line "[A BC D EF]" given a vector<string>
containing
"A", "BC", "D", "EF".

The following code is what I've written for this purpose:

#include <iostream>
#include <vector>
#include <iterator>
#include <sstream>

using namespace std;

int main()
{
vector<string> phonemes;
phonemes.push_back("A");
phonemes.push_back("BC");
phonemes.push_back("D");
phonemes.push_back("EF");

ostringstream s;

copy(phonemes.begin(), phonemes.end(),
ostream_iterator<string>(s, " "));

cout << "[" << s.str() << "]" << endl;

return EXIT_SUCCESS;
}

And this prints:
[A BC D EF ]

Please note the unnecessary space placed between "EF" and "]". How can
I modify
this code so that I can get [A BC D EF] in elegant way?

Sincerely,
Minkoo Seo
 
M

Marcus Kwok

Minkoo Seo said:
I'd like to print a line "[A BC D EF]" given a vector<string>
containing
"A", "BC", "D", "EF".

The following code is what I've written for this purpose:

#include <iostream>
#include <vector>
#include <iterator>
#include <sstream>

using namespace std;

int main()
{
vector<string> phonemes;
phonemes.push_back("A");
phonemes.push_back("BC");
phonemes.push_back("D");
phonemes.push_back("EF");

ostringstream s;

copy(phonemes.begin(), phonemes.end(),
ostream_iterator<string>(s, " "));

cout << "[" << s.str() << "]" << endl;

return EXIT_SUCCESS;
}

And this prints:
[A BC D EF ]

Please note the unnecessary space placed between "EF" and "]". How can
I modify
this code so that I can get [A BC D EF] in elegant way?

Maybe not the most elegant solutions, but I have solved this in two
different ways:

1. Output a '\b' to backspace over the last space (although sometimes
it can cause a strange character to be output, instead of actually
erasing the previous character).

2. Use an explicit loop:

// you need to verify that the index doesn't go out of bounds
ostringstream s;
s << phonemes[0];

// note that the loop index starts at 1
for (vector<string>::size_type i = 1; i != phonemes.size(); ++i) {
s << " " << phonemes;
}
 
B

bachelor

Minkoo Seo пиÑал(а):
Hi list.

I'd like to print a line "[A BC D EF]" given a vector<string>
containing
"A", "BC", "D", "EF".

The following code is what I've written for this purpose:

#include <iostream>
#include <vector>
#include <iterator>
#include <sstream>

using namespace std;

int main()
{
vector<string> phonemes;
phonemes.push_back("A");
phonemes.push_back("BC");
phonemes.push_back("D");
phonemes.push_back("EF");

ostringstream s;

copy(phonemes.begin(), phonemes.end(),
ostream_iterator<string>(s, " "));

cout << "[" << s.str() << "]" << endl;

return EXIT_SUCCESS;
}

And this prints:
[A BC D EF ]

Please note the unnecessary space placed between "EF" and "]". How can
I modify
this code so that I can get [A BC D EF] in elegant way?

Sincerely,
Minkoo Seo

May be something like:
string out = s.str();
cout << "[" << out.erase(out.size()-1) << "]" << endl;
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Minkoo said:
And this prints:
[A BC D EF ]

Please note the unnecessary space placed between "EF" and "]". How can
I modify
this code so that I can get [A BC D EF] in elegant way?

You can use something like the following:

class Separated
{
std::eek:stream & os;
bool first;
public:
Separated (std::eek:stream & osn) : os (osn), first (true) { }
void operator () (const std::string s)
{
if (first)
first= false;
else
os << ' ';
os << s;
}
}:


std::for_each (mylist.begin (), mylist.end (), Separated (std::cout) );

Or with some more code you can write a class for use with copy instead of
for_each, and make it a template if you want to generalize.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Julián Albo wrote:

Forgot to say: untested uncompiled code.
 
O

Old Wolf

Minkoo said:
copy(phonemes.begin(), phonemes.end(),
ostream_iterator<string>(s, " "));

Please note the unnecessary space placed between "EF" and "]". How can
I modify this code so that I can get [A BC D EF] in elegant way?

Not too elegant, but:

if ( !phonenes.empty() )
{
vector<string>::const_iterator last = phonemes.end();
--last;
copy(phonemes.begin(), last, ostream_iterator<string>(s, " "));
s << *last;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top