简体   繁体   English

std :: span.size()与数组/向量大小

[英]std::span.size() vs array/vector size

We are playing around with std::span() (using the gsl implementation for now) at work. 我们正在使用std::span() (目前使用gsl实现)。 Recently we discovered that comparing a std::span.size() to a vector.size() was giving a -Wsign-compare error : 最近,我们发现将std::span.size()vector.size()比较会产生-Wsign-compare error

if( span.size() > vector.size() ) // comparison between signed and unsigned integer expressions [-Wsign-compare]

I don't think we want to cast at every one of these comparisons. 我不认为我们想对这些比较中的每一个都进行比较。 Our coding guidelines treat these warnings as errors. 我们的编码准则将这些警告视为错误。 Curious if anyone has any ideas or suggestions? 好奇是否有人有任何想法或建议?

您可以使用迭代器,并且都可以使用函数std::distance()

if (std::distance(s.begin(), s.end()) > std::distance(v.begin(), v.end()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM