【问题标题】:C++ ostream out manipulationC++ ostream 输出操作
【发布时间】:2011-03-13 08:56:27
【问题描述】:

基本上它应该以这种格式列出所有矢量坐标:

(x, y, z)

但目前它确实是这样的 (x, y, z, )

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

我的代码:

    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 语句不是一个选项,只是好奇?
  • 我想 Jaanus 出于性能原因正在尝试使循环更紧密。在这种情况下,虽然不值得,但花在 IO 上的时间将远远超过花在测试循环索引上的时间。看看编译器是否可以在任何情况下优化测试将会很有趣。

标签: c++ ostream


【解决方案1】:

这是来自我的旧代码库。好的一面是:它带有单元测试:

为现代更新,更加通用和独立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()));
}

Live On Coliru

【讨论】:

  • 完美运行。太棒了。谢谢。
  • added 是一个未使用的变量
  • @Ian 哦,天哪。那是很久以前的事了。好地方,今天没有一个代码能通过我自己的代码审查。我将答案更新为更好一点:)
  • 请不要在以后如此大幅度地重写答案,而是发布一个新答案。对旧版本投票的 15 人不公平!
  • @LightnessRacesinOrbit 公平地说,他们过去这样做过,当时答案也稍微不那么过时了。在我的辩护中,我确保所有的测试用例仍然通过(你现在可以很容易地验证这一点,而不是以前的情况)。干杯:)
【解决方案2】:

使用 if 语句添加逗号

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

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

}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        这个怎么样:

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

        你没有 if 条件,但使用字符串重新分配的额外成本很小。

        【讨论】:

          猜你喜欢
          • 2019-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-20
          • 1970-01-01
          • 2012-02-14
          • 2014-12-03
          • 2011-08-12
          相关资源
          最近更新 更多