나는 당신이 최적화 할 이유를 당신이 당신의 프로그램에 경계를 많이 인쇄 할 필요가 있다고 가정하고 다음과 같이 쓸 무심, 그것은 등이 발생하기 쉬운 오류
입니다
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));
: 그냥 그렇게처럼 쓰기 그리고
#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]
: C에서는이 같은 매크로를 사용할 수 있습니다3210
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";
, 일부의 printf 구현을 허용 당신에게 옵션을 제공 새로운 포맷 변환 기능 - 예를 들어, http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html –
배열/벡터를 인쇄하는 일반적인 방법을 원하십니까? 아니면 문제는 ' printf'? – 101010
@ 40two. 여섯 가지 주장을 전달하는 것은 추한 것 같아요. 루프를 사용하지 않고 다른 방식으로 이것을 풀고 싶습니다. – Anna