如何使用ostream_iterator<>编译时的通用容器?

发布于 2024-10-09 17:57:23 字数 331 浏览 0 评论 0 原文

我想使用 copy() 打印出 T 类型容器的内容。我尝试过:

template<typename T>
void print_con( const T& con, const string& sep = ", ", const string& ms = "" ) {
 cout << ms << endl;
 copy( con.begin(), con.end(), ostream_iterator<?>( cout, sep ) ); 
}

我应该替换什么?和?

谢谢,

I would like to use copy() to print out the content of a container of type T. I tried:

template<typename T>
void print_con( const T& con, const string& sep = ", ", const string& ms = "" ) {
 cout << ms << endl;
 copy( con.begin(), con.end(), ostream_iterator<?>( cout, sep ) ); 
}

What should I replace ? with?

Thanks,
Chan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-10-16 17:57:23

您可以将容器的类型查找为 type

typename T::value_type

在这种情况下,我相信您想要的是

copy( con.begin(), con.end(), ostream_iterator<typename T::value_type>( cout, sep.c_str() ) );

希望这会有所帮助!

You can look up the type of a container as the type

typename T::value_type

In this case, I believe what you want is

copy( con.begin(), con.end(), ostream_iterator<typename T::value_type>( cout, sep.c_str() ) );

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文