将数组作为参数传递给printf

时间:2014-05-09 12:29:57

标签: c++ printf

有没有办法执行以下操作,但只将bounds传递给printf

double *bounds = getBounds();
printf("%f-%f, %f-%f, %f-%f",
    bounds[0], bounds[1],
    bounds[2], bounds[3],
    bounds[4], bounds[5] );

// what I'd like to write instead:
xxxprintf("%f-%f, %f-%f, %f-%f", bounds);

4 个答案:

答案 0 :(得分:2)

您可以自己编写xxxprintf()

#include <stdio.h>

int arrayprintf_dbl(const char *fmt, const double *data) {
  int n = 0;
  const char *p = fmt;
  const double *x = data;
  while (*p) {
    if (*p == '%') {
      // complicate as needed ...
      p++;
      if (*p != 'f') return -1; // error
      n += printf("%f", *x++);
    } else {
      putchar(*p);
      n++;
    }
    p++;
  }
  return n;
}

int main(void) {
  double bonus[6] = {1, 2, 3, 4, 5, 6};
  arrayprintf_dbl("%f-%f, %f-%f, %f-%f\n", bonus);
  return 0;
}

我在C中写道,我认为它可以很容易地转换为C ++(我不懂C ++)。

答案 1 :(得分:1)

我认为你想要优化它的原因是你需要在你的程序中打印很多边界,这样编写很麻烦,很容易出错等。


在C中,你可以使用这样的宏:

#define BOUNDS_FORMAT "%f-%f, %f-%f, %f-%f"
#define BOUNDS_ARG(b) b[0], b[1], b[2], b[3], b[4], b[5]

然后就这样写:

printf(BOUNDS_FORMAT, BOUNDS_ARG(bounds));
// ... some other code, then another call, with more text around this time:
printf("Output of pass #%d: " BOUNDS_FORMAT "\n", passNumber, BOUNDS_ARG(bounds));

在C ++中,您更期望使用std::cout或类似的流。然后你可以写一个自定义对象来为你做这个:

class PrintBounds {
  protected:
    const double* m_bounds;

  public:
    PrintBounds(const double* bounds)
      : m_bounds(bounds)
    {
    }

    friend std::ostream& operator<<(std::ostream& os, const PrintBounds& self)
    {
        os << self.m_bounds[0] << "-" << self.m_bounds[1] << ", "
           << self.m_bounds[2] << "-" << self.m_bounds[3] << ", "
           << self.m_bounds[3] << "-" << self.m_bounds[5];
        return os;
    }
};

然后你会像这样使用它:

std::cout << "Some other text: " << PrintBounds(bounds) << " ...\n";

答案 2 :(得分:1)

  • 我发布了一行打印算法的C ++ 11版本。我编写了一个与PairPrintFunctor相关联的仿函数(即for_each)可以打印具有偶数个元素的容器。如果容器包含奇数个元素,则忽略最后一个元素。您也可以设置自己的分隔符。

  • 注意但是,您无法避免迭代。在后台,由for_each引起了迭代过程。


#include <iostream>
#include <algorithm> 
#include <iterator>
#include <utility>
#include <memory>
#include <string>
#include <vector>

template<typename T>
class PairPrintFunctor
{
  std::size_t         _n;
  std::ostream       &_out;
  std::string         _delim;
  std::string         _sep;
  std::shared_ptr<T>   state;
public:
    explicit PairPrintFunctor(std::ostream &out, std::string delim = " ", std::string sep = " - ") : _n(0), _out(out), _delim(delim), _sep(sep) { }
    void operator()(T const &elem) 
    { 
        if(state == nullptr) {
          state.reset(new T(elem));
        } else {
          if (_n > 0) _out << _delim;
          _out << *state << _sep << elem;
          state.reset();
          state = nullptr;
          ++_n;
        }
    }
};

int main()
{
    int a[] {1, 2, 3, 4, 5, 6, 7, 8};
    std::for_each(std::begin(a), std::end(a), PairPrintFunctor<int>(std::cout, ", ", " --- "));
    std::cout << std::endl;
    std::vector<int> v{ 10, 20, 30, 40, 50, 60, 70, 80};
    std::for_each(std::begin(v), std::end(v), PairPrintFunctor<int>(std::cout, ", ", " --- "));
    std::cout << std::endl;
    return 0;
}

HTH

答案 3 :(得分:0)

不可能。参见:

http://en.cppreference.com/w/c/io/fprintf

或者,将代码分解为自己的函数。