关于begin和end函数

werun1 2014-08-20 04:11:02

已经用了iterator头文件,但是begin和end还是找不到。
#include<stdafx.h>
#include<iostream>
#include"Sales_item.h"
#include<string>
#include<iterator>
using std::string;
using std::cin;
using std::cout;
using std::endl;
#include<vector>
using std::vector;
using std::iterator;
int main()
{
int ia[]={0,1,2,3,4,5,6,7};
int *b=begin(ia);
int *e=end(ia);
for(int *k=b;k!=e;++k)
cout<<*k<<endl;
return 0;
}
...全文
2676 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_38100661 2017-03-28
  • 打赏
  • 举报
回复
13楼正解 +1 需要开命名空间 std::being(参数) std::end(参数) 这个就ok!!
「已注销」 2017-03-05
  • 打赏
  • 举报
回复
13楼正解!
ID870177103 2016-07-18
  • 打赏
  • 举报
回复
template <class ITEM ,int SIZE>
struct Iteratable {
private:
	ITEM mArray[SIZE] ;

public:
	Iteratable () = delete ;

	ITEM *begin () {
		return &mArray[0] ;
	}

	const ITEM *begin () const {
		return &mArray[0] ;
	}

	ITEM *end () {
		return &mArray[SIZE - 1] ;
	}

	const ITEM *end () const {
		return &mArray[SIZE - 1] ;
	}

	static Iteratable &from (ITEM (&left)[SIZE]) {
		return reinterpret_cast<Iteratable &> (left) ;
	}
} ;

int main () {
	int a[10] ;
	for (int i = 0 ; i < 10 ; i++)
		a[i] = i ;
	for (int &i : Iteratable<int ,10>::from (a))
		std::cout << i << " " ;
	return 0 ;
}
个人推荐
ID870177103 2016-07-18
  • 打赏
  • 举报
回复


template<class _Ty,
	size_t _Size> inline
	_CONST_FUN _Ty *begin(_Ty (&_Array)[_Size]) _NOEXCEPT
	{	// get beginning of array
	return (_Array);
	}

template<class _Ty,
	size_t _Size> inline
	_CONST_FUN _Ty *end(_Ty (&_Array)[_Size]) _NOEXCEPT
	{	// get end of array
	return (_Array + _Size);
	}
ID870177103 2016-07-18
  • 打赏
  • 举报
回复
template<class _Container>
	auto inline begin(_Container& _Cont) -> decltype(_Cont.begin())
	{	// get beginning of sequence
	return (_Cont.begin());
	}

template<class _Container>
	auto inline begin(const _Container& _Cont) -> decltype(_Cont.begin())
	{	// get beginning of sequence
	return (_Cont.begin());
	}

template<class _Container>
	auto inline end(_Container& _Cont) -> decltype(_Cont.end())
	{	// get end of sequence
	return (_Cont.end());
	}

template<class _Container>
	auto inline end(const _Container& _Cont) -> decltype(_Cont.end())
	{	// get end of sequence
	return (_Cont.end());
	}
C++11标准对这函数的定义
imkelt 2016-07-18
  • 打赏
  • 举报
回复
其实标准库的begin和end两个函数是定义在std中的,在这两个前面加上作用域就可以了,std::begin()和std::end().
sinat_20953243 2016-04-17
  • 打赏
  • 举报
回复
楼上们怎么都不习惯用全局命名域啊。每一个都要std:xx一遍!
starytx 2016-02-17
  • 打赏
  • 举报
回复
我学的一般都是 xx.begin() ,xx.end() 没见过你这样用的
二中英雄 2016-02-14
  • 打赏
  • 举报
回复
13楼正解!最近我也遇到了相同情况,解决了
lm_whales 2015-12-10
  • 打赏
  • 举报
回复
C++11,14的特征 需要编译器支持
伊莉伊莉雅 2015-12-10
  • 打赏
  • 举报
回复
需要使用命名空间std,c++primer后面默认using std的。
// std::begin / std::end example
#include <iostream>     // std::cout
#include <vector>       // std::vector, std::begin, std::end

int main () {
  int foo[] = {10,20,30,40,50};
  std::vector<int> bar;

  // iterate foo: inserting into bar
  for (auto it = std::begin(foo); it!=std::end(foo); ++it)
    bar.push_back(*it);

  // iterate bar: print contents:
  std::cout << "bar contains:";
  for (auto it = std::begin(bar); it!=std::end(bar); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
菜鸟来了2022 2014-08-26
  • 打赏
  • 举报
回复
楼主顶起。!!!!!!!!!!!!!!!!
老王爱上猫 2014-08-25
  • 打赏
  • 举报
回复
反转灵魂 2014-08-21
  • 打赏
  • 举报
回复
楼主,麻烦逆推一下: 假设你的begin() 是返回数组首地址,end() 是返回末端地址。 因为传出的是指向int类型的指针,所以返回的也是指向int型的指针。 成立。 for循环是int型,成立。 输出*k 。到这里为止,都没有错。 前提是 begin() ,end() 是你自己写的。 比如return ia+length; 如果你要用别人的方法,比如vector里面的这两个方法,你得声明对象调用: vector<int> ivec(ia,ia+7); ivec.begin(); ivec.end(); 但是他们返回的不是指向int型的,而是指向vector<int>::iterator迭代器类型。 所以你不能用int* 来接受这个地址,而是: vector<int>::iterator beg=ivec.begin(); vector<int>::iterator end=ivec.end(); 觉得详细给好评!
KangRoger 2014-08-21
  • 打赏
  • 举报
回复
iterator.h只是讲解迭代器具体的实现,begin和end定义在容器里面的,楼主搞混了吧
cdpcsc 2014-08-20
  • 打赏
  • 举报
回复
你的编译器不支持c++11 begin是c++11才引入的 定义在 <iterator>头文件里。
Saleayas 2014-08-20
  • 打赏
  • 举报
回复
int *b=&ia[0]; int *e=&ia[sizeof(ia)]; for(int *k=b;k!=e;++k) cout<<*k<<endl;
神奕 2014-08-20
  • 打赏
  • 举报
回复
引用 5 楼 lisong694767315 的回复:
C++11才允许对数组使用迭代器

#include<iostream>
#include<iterator>
using namespace std;

int main()
{
	int ia[]={0,1,2,3,4,5,6,7};
	auto b = begin(ia);  // C++11才允许对数组使用迭代器
	auto e= end(ia);
	for(auto k=b ; k!=e; ++k)
		cout << *k << endl;
	 return 0;
}
输出:

$ g++ -std=c++11 -o test test.cpp 
$ ./test 
0
1
2
3
4
5
6
7
神奕 2014-08-20
  • 打赏
  • 举报
回复
C++11才允许对数组使用迭代器

#include<iostream>
#include<iterator>
using namespace std;

int main()
{
	int ia[]={0,1,2,3,4,5,6,7};
	auto b = begin(ia);  // C++11才允许对数组使用迭代器
	auto e= end(ia);
	for(auto k=b ; k!=e; ++k)
		cout << *k << endl;
	 return 0;
}
赵4老师 2014-08-20
  • 打赏
  • 举报
回复
加载更多回复(3)

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧