Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ ostream out manipulation

Tags:

c++

ostream

well basically it should list all the vector coords in this kind of format :

(x, y, z)

but at the moment it does like this (x, y, z, )

easiest way would be using if in the for cycle, but can i substract a small piece of string from the out variable?

my code:

    template <unsigned short m>
    std::ostream& operator<<(std::ostream& out, const Vector<m>& v) {
    out << "(";
    for(int i = 0; i < m; i++) {
        out << v.coords[i] << ", ";
    }
    out << ")";
    return out;
}
like image 930
Jaanus Avatar asked Mar 13 '11 08:03

Jaanus


4 Answers

This is from an old code base of mine. On the upside: it comes with unit tests:

Updated for modern times, more generic and self contained Live On Coliru

/*! note: delimiter cannot contain NUL characters
 */
template <typename Range, typename Value = typename Range::value_type>
std::string Join(Range const& elements, const char *const delimiter) {
    std::ostringstream os;
    auto b = begin(elements), e = end(elements);

    if (b != e) {
        std::copy(b, prev(e), std::ostream_iterator<Value>(os, delimiter));
        b = prev(e);
    }
    if (b != e) {
        os << *b;
    }

    return os.str();
}

/*! note: imput is assumed to not contain NUL characters
 */
template <typename Input, typename Output, typename Value = typename Output::value_type>
void Split(char delimiter, Output &output, Input const& input) {
    using namespace std;
    for (auto cur = begin(input), beg = cur; ; ++cur) {
        if (cur == end(input) || *cur == delimiter || !*cur) {
            output.insert(output.end(), Value(beg, cur));
            if (cur == end(input) || !*cur)
                break;
            else
                beg = next(cur);
        }
    }
}

And some corresponding unit test cases:

void testSplit() {
    std::vector<std::string> res;
    const std::string test = "a test ,string, to,,,be, split,\"up,up\",";
    TextUtils::Split(',', res, test);

    UT_EQUAL(10u, res.size());
    UT_EQUAL("a test ", res[0]);
    UT_EQUAL("string", res[1]);
    UT_EQUAL(" to", res[2]);
    UT_EQUAL("", res[3]);
    UT_EQUAL("", res[4]);
    UT_EQUAL("be", res[5]);
    UT_EQUAL(" split", res[6]);
    UT_EQUAL("\"up", res[7]); // Thus making 'split' unusable for parsing
    UT_EQUAL("up\"", res[8]); //  csv files...
    UT_EQUAL("", res[9]);

    TextUtils::Split('.', res, "dossier_id");
    UT_EQUAL(11u, res.size());

    res.clear();
    UT_EQUAL(0u, res.size());

    TextUtils::Split('.', res, "dossier_id");
    UT_EQUAL(1u, res.size());
    std::string UseName = res[res.size() - 1];
    UT_EQUAL("dossier_id", UseName);
}

void testJoin() {
    std::string elements[] = { "aap", "noot", "mies" };

    typedef std::vector<std::string> strings;

    UT_EQUAL(""               , TextUtils::Join(strings(), ""));
    UT_EQUAL(""               , TextUtils::Join(strings(), "bla"));
    UT_EQUAL("aap"            , TextUtils::Join(strings(elements, elements + 1), ""));
    UT_EQUAL("aap"            , TextUtils::Join(strings(elements, elements + 1), "#"));
    UT_EQUAL("aap"            , TextUtils::Join(strings(elements, elements + 1), "##"));
    UT_EQUAL("aapnoot"        , TextUtils::Join(strings(elements, elements + 2), ""));
    UT_EQUAL("aap#noot"       , TextUtils::Join(strings(elements, elements + 2), "#"));
    UT_EQUAL("aap##noot"      , TextUtils::Join(strings(elements, elements + 2), "##"));
    UT_EQUAL("aapnootmies"    , TextUtils::Join(strings(elements, elements + 3), ""));
    UT_EQUAL("aap#noot#mies"  , TextUtils::Join(strings(elements, elements + 3), "#"));
    UT_EQUAL("aap##noot##mies", TextUtils::Join(strings(elements, elements + 3), "##"));
    UT_EQUAL("aap  noot  mies", TextUtils::Join(strings(elements, elements + 3), "  "));

    UT_EQUAL("aapnootmies"    , TextUtils::Join(strings(elements, elements + 3), "\0"));
    UT_EQUAL("aapnootmies"    , TextUtils::Join(strings(elements, elements + 3), std::string("\0" , 1).c_str()));
    UT_EQUAL("aapnootmies"    , TextUtils::Join(strings(elements, elements + 3), std::string("\0+", 2).c_str()));
    UT_EQUAL("aap+noot+mies"  , TextUtils::Join(strings(elements, elements + 3), std::string("+\0", 2).c_str()));
}

See it Live On Coliru

like image 159
sehe Avatar answered Nov 17 '22 23:11

sehe


Use an if statement to add the comma

for(int i = 0;i<m;i++)
{
  out<<V.coords[i];

  if(i !=m-1)
     out<<",";

}
like image 39
jonsca Avatar answered Nov 18 '22 01:11

jonsca


That isn't possible. Another possibility: moving out of the loop the output of the first or the last coordinates. Then there is no need of an if (or the operator ?:) inside the loop, but handling an empty vector is more complex as it will need an if outside the loop.

like image 3
AProgrammer Avatar answered Nov 18 '22 01:11

AProgrammer


Loop from i to m-1 printing the value and a comma, then at the end of the loop (where you print out the ")"), print out the last element without a comma

like image 2
deek0146 Avatar answered Nov 18 '22 01:11

deek0146