C++begin和end运算符的返回迭代器的类型如何判断?
时间:2020-10-29 14:25:42|栏目:C代码|点击: 次
begin和end返回的具体类型应该由对象是否是常量进行确定,如果对象是常量,则这两个函数返回const_iterator;
如果对象不是常量,则这个函数返回iterator类型。下面利用一个超级简单的小程序进行验证二者的类型,源代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
const vector<int> cv;
auto it1 = ivec.begin();
auto it2 = cv.begin();
cout<<typeid(it1).name()<<endl;
cout<<typeid(it2).name()<<endl;
system("pause");
}

从输出结果,可以很好的验证上面的分析是正确的。
总结
栏 目:C代码
下一篇:C语言实现计算树的深度的方法
本文标题:C++begin和end运算符的返回迭代器的类型如何判断?
本文地址:http://www.codeinn.net/misctech/17113.html






