C++输出流(ostream)的操作技巧

5

基本上,它应该以以下格式列出所有向量坐标:

(x,y,z)

但目前显示为 (x,y,z,)

最简单的方法是在 for 循环中使用 if,但我可以从输出变量中减去一个小字符串吗?

我的代码:

    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;
}

为什么if语句不是一个选项,只是好奇? - jonsca
我想 Jaanus 试图为了性能原因使循环更紧凑。但在这种情况下,这是不值得的,因为执行 IO 的时间远远超过测试循环索引的时间。有趣的是看看编译器是否可以优化出任何情况下的测试。 - Himadri Choudhury
5个回答

21

这是我旧代码库中的代码。好处是:它带有单元测试:

已更新以适应现代化需求,更通用和自包含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);
        }
    }
}

还有一些相应的单元测试用例:

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()));
}

点击此处查看实时演示


运行完美无缺。超级棒。谢谢。 - Yash
"added" 是一个未使用的变量。 - Ian
@Ian 哦,那是很久以前的事了。好发现,但是那些代码都不会通过我的代码审查。我更新了答案,让它更好一些 :) - sehe
请不要在很长时间后大幅改写答案,而是发布一个新的答案。这对于投票支持旧版本的15个人来说是不公平的! - Lightness Races in Orbit
@LightnessRacesinOrbit,公平地说,他们过去确实这样做过,当时答案也稍微不那么过时。而且为了辩护自己,我确保所有的测试用例仍然通过了(现在你可以轻松验证这一点,而不是像以前那样)。干杯 :) - sehe
Join 函数中,我不理解为什么要进行第二次 (b != e) 的检查。如果你只是将 b = prev(e),那么 b 就永远不会等于 e。在我看来,你可以用 os << *prev(e) 替换 b = prev(e),并省略第二个 if 语句。(还有,真的需要写 using namespace std; 吗?) - JHBonarius

5
使用if语句添加逗号。
for(int i = 0;i<m;i++)
{
  out<<V.coords[i];

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

}

3

这是不可能的。另一个可能性是将第一个或最后一个坐标的输出移出循环。然后在循环内部就不需要使用if(或运算符?:),但处理空向量会更加复杂,因为它需要在循环外部使用if语句。


2
从 i 循环到 m-1,打印出该值和逗号,然后在循环结束时(在您打印“)”的位置),打印出最后一个元素而不带逗号。

0
这个怎么样?
template <unsigned short m>
std::ostream& operator<<(std::ostream& out, const Vector<m>& v) {
   std::string sep;
   out << "(";
   for(int i = 0; i < m; i++) {
      out << sep << v.coords[i];
      sep = ", ";
   }
   out << ")";
   return out;
}

你将不会得到任何的条件语句,但会以稍微增加使用字符串重新赋值的代价为代价。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接