C++ 错误C2760:语法错误:意外标记'<';,预期的'';
使用VS2017和代码:C++ 错误C2760:语法错误:意外标记'<';,预期的'';,c++,C++,使用VS2017和代码: template <typename T> void showset(vector<T> v) { for (vector<T>::iterator it = v.begin(); it != v.end(); it++) { cout << *it; } cout << endl; } 模板 无效显示集(向量v) { for(vector::iterator
template <typename T>
void showset(vector<T> v)
{
for (vector<T>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it;
}
cout << endl;
}
模板
无效显示集(向量v)
{
for(vector::iterator it=v.begin();it!=v.end();it++)
{
cout首先注意,如果在这里引用一个类似的vector::iterator
,那么您需要将typename
放在前面。此外,取决于T
是什么,只有当std::cout
的操作符对此的正确解释如下:
template <typename T>
void showset(const T& v)
{
for (auto const &x : v)
{
cout << x;
}
cout << endl;
}
template<typename T>
class LogContainerHelper {
LogContainerHelper(const T& v
size_t maxFront,
size_t maxTail)
: mContainer{ v }
, mMaxFront{ maxFront }
, mMaxTail{ maxTail }
{}
std::ostream &printTo(std::ostream &out) const {
// here I usually have something more complex
// depending on mMaxFront and mMaxTail values,
// don't have time to recreate that now
auto it = std::begin(mContainer);
auto end = std::end(mContainer);
out << '[';
if (it != end) {
out << *it;
++it;
}
for (; it != end; ++it)
{
out << ", " << *it;
}
return out << ']';
}
private:
const T &mContainer;
size_t mMaxFront;
size_t mMaxTail;
};
template<typename T>
std::ostream &operator <<(std::ostream &out, const LogContainerHelper<T> &helper) {
return helper.printTo(out);
}
template<typename T>
auto LogContainer(const T& v,
size_t maxFront = std::numeric_limits<size_t>::max(),
size_t maxTail = 0)
-> LogContainerHelper<T> {
return LogContainerHelper<T>{ v, maxFront, maxTail };
}
模板
无效展示集(const T&v)
{
用于(自动常数和x:v)
{
请尝试使用typename vector::iterator it=v.begin();
,或者auto it=v.begin();
。此外,传递容器等大对象时,不要按值传递,而是按常量引用传递:void showset(const vector&v)
。这与OP的void showset(vector v)不同
是的,这是故意的。这更通用,它适用于任何容器。我同意它更通用,只是说这种通用性可能不是OP想要的。如果需要,限制也是一个功能。至少我没有想到他在哪里指定它不是这样。@Bllue如果你想知道w为什么不使用名称空间std;
来放置,欢迎您阅读。
template <typename T>
void showset(std::vector<T> v)
{
for (auto& ref : v)
{
std::cout << ref;
}
std::cout << std::endl;
}
void showset(const std::vector<T>& v);
for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
template <typename T>
void showset(const T& v)
{
for (auto const &x : v)
{
cout << x;
}
cout << endl;
}
template <typename T>
void showset(const T& v)
{
for (auto it = std::begin(v); it != std::end(v); ++it)
{
cout << *it;
}
cout << endl;
}
template<typename T>
class LogContainerHelper {
LogContainerHelper(const T& v
size_t maxFront,
size_t maxTail)
: mContainer{ v }
, mMaxFront{ maxFront }
, mMaxTail{ maxTail }
{}
std::ostream &printTo(std::ostream &out) const {
// here I usually have something more complex
// depending on mMaxFront and mMaxTail values,
// don't have time to recreate that now
auto it = std::begin(mContainer);
auto end = std::end(mContainer);
out << '[';
if (it != end) {
out << *it;
++it;
}
for (; it != end; ++it)
{
out << ", " << *it;
}
return out << ']';
}
private:
const T &mContainer;
size_t mMaxFront;
size_t mMaxTail;
};
template<typename T>
std::ostream &operator <<(std::ostream &out, const LogContainerHelper<T> &helper) {
return helper.printTo(out);
}
template<typename T>
auto LogContainer(const T& v,
size_t maxFront = std::numeric_limits<size_t>::max(),
size_t maxTail = 0)
-> LogContainerHelper<T> {
return LogContainerHelper<T>{ v, maxFront, maxTail };
}
cout << "Main containter is: " << LogContainer(v) << '\n';