gpt4 book ai didi

c++ - 递归 AsString() 在 C++ 中打印 STL 容器

转载 作者:太空狗 更新时间:2023-10-29 21:07:32 25 4
gpt4 key购买 nike

我正在尝试编写一个 AsString() 函数,根据我的喜好将 STL 容器转换为字符串。这是我到目前为止提出的代码:

template<class T>
inline string AsString(const T& v);

template<class First, class Second>
inline string AsString(const pair<First, Second>& p);

template<class Iter>
inline string PrintSequence(const char* delimiters, Iter begin, Iter end) {
string result;
result += delimiters[0];
int size = 0;
for (size = 0; begin != end; ++size, ++begin) {
if (size > 0) {
result += ", ";
}
result += AsString(*begin);
}
result += delimiters[1];
result += StringPrintf("<%d>", size);
return result;
}

#define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
template<class T1, class T2> \
inline string AsString(const Sequence<T1, T2>& seq) { \
return PrintSequence("[]", seq.begin(), seq.end()); \
}

OUTPUT_TWO_ARG_CONTAINER(vector)
OUTPUT_TWO_ARG_CONTAINER(deque)
OUTPUT_TWO_ARG_CONTAINER(list)

template<class First, class Second>
inline string AsString(const pair<First, Second>& p) {
return "(" + AsString(p.first) + ", " + AsString(p.second) + ")";
}

template<class T>
inline string AsString(const T& v) {
ostringstream s;
s << v;
return s.str();
}

如您所见,基本思想是 AsString()在 STL 容器上递归调用自身,然后它触底到通常的 operator<<() (我不想覆盖 operator<<() 的原因是因为我不想干扰其他完全这样做的库)。

现在,AsString()在浅层容器上编译和工作,但不在嵌套容器上:

vector<int> v;
v.push_back(1);
v.push_back(2);
AsString(v) == "[1, 2]<2>"; // true

vector<vector<int> > m;
m.push_back(v);
m.push_back(v);
AsString(m) == "[[1, 2]<2>, [1, 2]<2>]<2>"; // Compilation Error!!!

出于某种原因,编译器想要使用 operator<<()在尝试打印 `m' 的元素时,尽管我已经为 vector 提供了模板特化。

我如何制作 AsString()工作?

更新:好的,事实证明定义的顺序很重要(至少对于这个编译器 -- gcc 4.4.3)。当我把宏定义放在第一位时,编译器会正确地拾取它们并显示一个 vector 的 vector 。莫名其妙。

最佳答案

模板世界是美好的……也是一个真正的陷阱……

一种特化,采用现有的模板函数并指定其所有参数。

重载是为一组不同的参数重用与另一个函数(无论是否为模板)相同的名称。

template <typename T>
void foo(T const& t);

template <>
void foo<int>(int i); // this is a "complete" specialization

template <typename T, typename U>
void foo<std::pair<T,U>>(std::pair<T,U> const& pair);
// this is a "partial" specialization
// and by the way... it does NOT COMPILE

template <typename T, typename U>
void foo(std::pair<T,U> const& pair); // this is an overload

注意语法差异,在重载中没有 <xxxx>在标识符之后(此处为 foo)。

在 C++ 中,不可能部分特化一个函数;那就是在论点中保留一些通用性。您可以重载或完全特化:此时必读GotW #49: Template Specialization and Overloading

因此,选择是:

template <typename T>
std::string AsString(const T& v); // (1)

template <typename T, typename Allocator>
std::string AsString(std::vector<T, Allocator> const& v); // (2)

真正的问题是:*begin 的类型是什么? ?

嗯,m不是 const 限定的:

  • Iter逻辑上是 std::vector< std::vector<int> >::iterator .
  • *begin 的类型因此是std::vector<int>&

所以这两个重载被认为是:

  • (1):T = std::vector<int> , 需要转换为 const-ref
  • (2): T = int, U = std::allocator<int> , 需要转换为 const-ref

应该选择第二种,因为据我了解,它更接近真实类型。我用 VC++ 2010 测试了它,它确实被选中了。

您是否也可以声明 vector 重载的非 const 限定版本,看看它是否能让您的编译器满意? (顺便说一句,我想知道它的名字 ;))。

关于c++ - 递归 AsString() 在 C++ 中打印 STL 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com