- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试编写一个 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>&
所以这两个重载被认为是:
T = std::vector<int>
, 需要转换为 const-refT = int, U = std::allocator<int>
, 需要转换为 const-ref应该选择第二种,因为据我了解,它更接近真实类型。我用 VC++ 2010 测试了它,它确实被选中了。
您是否也可以声明 vector 重载的非 const 限定版本,看看它是否能让您的编译器满意? (顺便说一句,我想知道它的名字 ;))。
关于c++ - 递归 AsString() 在 C++ 中打印 STL 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820022/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
以下 C++ 代码和 Makefile 产生了一个无法理解的编译错误(对我来说)。谁能解释一下 问题到底是什么? 修复此代码需要做什么?能举个例子吗? 我在 Cygwin 的 GCC 上成功编译了这段
我大量使用 BOOST_FOREACH 来迭代容器,并且由于我最近转向 c++0x,我认为我可以用基于范围的 替换 BOOST_FOREACH >for 构造。下面这段代码 #include #inc
我编写了代码,允许按照输入的顺序遍历映射数据。 我编写了几次代码的解决方案是: 给定键类型 K 和数据类型 D, 标准:: map std::向量 如果想随机查找数据条目,请使用 map.find(K
我在 cygwin 上使用 gcc 3.4.4。我在下面的代码中收到了这个相当令人费解的 STL 错误消息,它根本不使用 STL: #include using namespace std; con
我正在使用 STL 函数 count_if 来计算所有正值在 double vector 中。例如我的代码是这样的: vector Array(1,1.0) Array.push_back(-1.
我正在尝试使用 numpy-STL 从 STL 模型中提取顶点以用于相干点漂移注册。你如何提取顶点?我了解如何从顶点和面列表创建网格,但不了解如何倒退。 我试过:从顶点和面创建一个新网格。导入创建的网
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html 根据那篇文章,STL 不适合游戏开发。 你对此有何看法? 我目前的
在幕后,STL 映射是一棵红黑树,它使用其键的 typename _Rb_tree::iterator _Rb_tree:: find(const _Key& __k) { iterator _
我对 C++ 有很好的了解,但从未深入研究 STL。我必须学习 STL 的哪一部分才能提高工作效率并减少工作中的缺陷? 谢谢。 最佳答案 I have good knowledge of C++ 恕我
map rollCallRegister; map :: iterator rollCallRegisterIter; pair , bool> returnPair; rollCallRegi
在查看一些算法的模板名称时, 我看到这个名字对应于一个图书馆的概念。 取std::mismatch例如。 template std::pair mismatch( InputIt1 first1, I
我想对 class Person 的对象数组进行排序基于其数据成员' age '.我将对象存储在 vector v 中. 据我所知,至少有 4 种方法可以执行此操作,根据下面编写的方法,我有以下问题。
我对 gcc 或 Visual Studio 打包之外的 STL 实现感到好奇,因此快速 Google 搜索出现了一些结果,例如: Apache stdcxx uSTL rdeSTL 在什么情况下应该
我可以使用例如std::vector吗?在 macOs/XCode 的 DriverKit 驱动程序中? DriverKit 有一些容器类,如 OSArray https://developer.ap
我找不到任何关于如何将范围与容器结合使用的好文档。我正在尝试使用给定的 .insertAfter() 函数将一个元素插入到 SList 中。它需要一个范围,但我不知道如何检索它。 有人可以发布一两个示
如何在(例如)STL 容器中引入聚合初始化支持以正确构造它们?我的意思是: struct A { int i; char c; }; std::list l; // empty l.insert(st
我有一个 STL map : std::map > my_map; 我有两个变量: string name; int age; 这些变量的值发生变化,但本质上我想要做的是: 如果键名不存在,则创建键名
我是 C++ 的新手,请求帮助解决问题。 我正在编写一个简单的 STL 样式函数,它应该返回序列的中间元素( vector 、列表等) 这是我的函数,我尝试使用迭代器的概念 template It
如果我将几个水果名称推回第一个STL列表,同时,我将每个水果的编号推回第二个STL列表;如果我想按字母顺序对第一个STL列表进行排序,我该如何按水果STL列表的顺序对第二个STL列表进行排序? 最佳答
我是一名优秀的程序员,十分优秀!