百度了一圈啥玩意都没找到,还是谷歌靠谱.

像std::string数组用sizeof并不太准确.百度找了一大圈,没有好办法.还是靠谷歌.

 

std::string  abc [] = {"test","test2","test3","test4"};

 

 

 

用宏求

 

//C++11

template < typename T, std::size_t N >
constexpr std::size_t size( T(&)[N] ) { return N ; }


std::cout << "array 'abc' size: " << size(abc) << ' ' ;

 

 

用std::end求

 

//C++11


std::cout << std::end(abc) - std::begin(abc) << ' ' ;

 

 

 

通过维度计算

 

//C++11


std::cout << std::extent< decltype(abc) >::value << ' ' ;

 

向量类型计算

 

这个我也没搞懂.先贴出来.

 

// C++11 


#include <vector>
#include <string>

std::vector vs {"a", "be", "see"}; 
std::size_t length = vs.size();

 

for枚举

 

当然,效率不高.

这个可以在for内直接枚举.

 

//c++ 11

int count = 0;

for (auto v : abc)
{
        
        //直接用 v 参与计算,这里 v等价于 abc[count];

        //先干其他的.再累加.
        count++;
         
}

 

 

 

参考

 

http://www.cplusplus.com/forum/general/110091/#

https://blog.csdn.net/u010196624/article/details/90085547

https://www.cnblogs.com/developing/articles/10890903.html