将数组作为参数传递给printf
Pass array as argument to 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);
你可以自己写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-%fn", bonus);
return 0;
}
我用C写的,我想它可以很容易地转换成c++(我不懂c++)
我认为你想要优化它的原因是你需要在你的程序中打印大量的边界,这样写是令人厌烦的,它很容易出错等。
在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";
-
我发布了一个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
不可能。看到:
http://en.cppreference.com/w/c/io/fprintf相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将__device__ lambda 作为参数传递给 __global__ 函数