Printing vector<string> with spaces between items.

Discussion in 'C++' started by Minkoo Seo, Jul 24, 2006.

  1. Minkoo Seo

    Minkoo Seo Guest

    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
     
    Minkoo Seo, Jul 24, 2006
    #1
    1. Advertisements

  2. Minkoo Seo

    Marcus Kwok Guest

    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;
    }
     
    Marcus Kwok, Jul 24, 2006
    #2
    1. Advertisements

  3. Minkoo Seo

    bachelor Guest

    Minkoo Seo пиÑал(а):
    May be something like:
    string out = s.str();
    cout << "[" << out.erase(out.size()-1) << "]" << endl;
     
    bachelor, Jul 24, 2006
    #3
  4. 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, Jul 24, 2006
    #4
  5. Julián Albo wrote:

    Forgot to say: untested uncompiled code.
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jul 24, 2006
    #5
  6. Minkoo Seo

    Old Wolf Guest

    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;
    }
     
    Old Wolf, Jul 25, 2006
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.