【发布时间】:2013-09-15 12:07:08
【问题描述】:
我有一组不同类型的容器,每个容器都与一个字符串 id 相关联。如果关联的容器不为空,则以下函数应打印 id。
如果我想将 std::vector 的大小传递给函数,我应该将它作为 size_type 对象传递吗?像这样:
void printIfNotEmpty(const std::string& id, size_type sizeOfContainer)
{
if(sizeOfContainer)
{
output << id << " is not empty";
}
else
{
output << id << " is empty";
}
}
如果是这样,size_type 在哪个命名空间中?如何在我的代码中包含它的定义?
也许这是一个解决方案:
template<class T>
void printIfNotEmpty(const std::string& id, const T& container)
{
if(container.size())
{
output << id << " is not empty";
}
else
{
output << id << " is empty";
}
}
【问题讨论】:
-
是成员 typedef:
std::vector<T>::size_type. -
你的意思是你想传递一个字符串的大小?
-
@billz 不,另一个容器
-
你为什么要传递大小?您可以从传递的向量或字符串中获取它。
-
您仍然不需要将尺寸作为单独的参数传递。
标签: c++