所有具有特征的容器类型的模板begin()end()size()(template for all container types with features begin() end() size())
为矢量编写以下模板代码。 它不会编译,因为一个调用是使用列表而不是向量。 我想重写模板,以便它适用于任何容器。 对该容器的限制是它需要包含一个类型T,有一个begin(),有一个end()并且有一个大小();
这可以用模板完成吗? 我会怎么做?
#include <iostream> #include <string> #include <vector> #include <list> #include <typeinfo> namespace std { template <typename T> string to_string(T s) { return typeid(T).name(); } string to_string(string s) { return s; } } template<typename T, typename A = std::allocator<T>> static std::string range_to_string(std::vector<T, A> const& vec) { bool more = false; std::string str = "size:" + std::to_string(vec.size()) + " ["; for (const T & item : vec) { if (more) { str += ", "; } str += std::to_string(item); more = true; } str += "]"; return str; } void demo1() { std::string dat[] = {"one", "two", "three"}; std::vector<std::string> vs(dat, dat + 3); std::cout << range_to_string(vs) << std::endl; } void demo2() { std::list<short> ls= {1, 2, 3, 4}; std::cout << range_to_string(ls) << std::endl; } int main() { demo1(); demo2(); return 0; }The following template code is written for vectors. It does not compile because of one call is using list rather than vector. I want to rewrite the template so that it works for any container. The restrictions on that container are that it needs to contain a type T, have a begin(), have an end() and have a size();
Can this be done with templates? How would I do that?
#include <iostream> #include <string> #include <vector> #include <list> #include <typeinfo> namespace std { template <typename T> string to_string(T s) { return typeid(T).name(); } string to_string(string s) { return s; } } template<typename T, typename A = std::allocator<T>> static std::string range_to_string(std::vector<T, A> const& vec) { bool more = false; std::string str = "size:" + std::to_string(vec.size()) + " ["; for (const T & item : vec) { if (more) { str += ", "; } str += std::to_string(item); more = true; } str += "]"; return str; } void demo1() { std::string dat[] = {"one", "two", "three"}; std::vector<std::string> vs(dat, dat + 3); std::cout << range_to_string(vs) << std::endl; } void demo2() { std::list<short> ls= {1, 2, 3, 4}; std::cout << range_to_string(ls) << std::endl; } int main() { demo1(); demo2(); return 0; }最满意答案
我在这里找到了我的答案。
看起来这是常识。
#include <iostream> #include <string> #include <vector> #include <list> #include <typeinfo> namespace std { template <typename T> string to_string(T s) { return typeid(T).name(); } string to_string(short s) { return to_string((int) s); } string to_string(string s) { return s; } } template<typename T, typename A = std::allocator<T>, template <typename, typename> class container> static std::string range_to_string(container<T, A> const& vec) { bool more = false; std::string str = "size:" + std::to_string(vec.size()) + " ["; for (const T & item : vec) { if (more) { str += ", "; } str += std::to_string(item); more = true; } str += "]"; return str; } void demo1() { std::string dat[] = {"one", "two", "three"}; std::vector<std::string> vs(dat, dat + 3); std::cout << range_to_string(vs) << std::endl; } void demo2() { std::list<short> ls= {1, 2, 3, 4}; std::cout << range_to_string(ls) << std::endl; } int main() { demo1(); demo2(); return 0; }I found my answer here.
It looks like this is common knowledge.
#include <iostream> #include <string> #include <vector> #include <list> #include <typeinfo> namespace std { template <typename T> string to_string(T s) { return typeid(T).name(); } string to_string(short s) { return to_string((int) s); } string to_string(string s) { return s; } } template<typename T, typename A = std::allocator<T>, template <typename, typename> class container> static std::string range_to_string(container<T, A> const& vec) { bool more = false; std::string str = "size:" + std::to_string(vec.size()) + " ["; for (const T & item : vec) { if (more) { str += ", "; } str += std::to_string(item); more = true; } str += "]"; return str; } void demo1() { std::string dat[] = {"one", "two", "three"}; std::vector<std::string> vs(dat, dat + 3); std::cout << range_to_string(vs) << std::endl; } void demo2() { std::list<short> ls= {1, 2, 3, 4}; std::cout << range_to_string(ls) << std::endl; } int main() { demo1(); demo2(); return 0; }更多推荐
发布评论