关于using std::和using namespace std;

hlsoven 2009-12-30 12:10:15
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::vector<int>::iterator;

int main()
{
vector<int> container;

for(int i=0; i<=4; i++)
container.push_back(i);

cout<<"Here is what is in the container:\n";
iterator p;
for(p = container.begin(); p != container.end(); p++)
cout<<*p<<" ";
cout<<endl;

cout<<"Setting entries to 0:\n";
for(p = container.begin(); p!=container.end(); p++)
*p=0;

for(p = container.begin(); p != container.end(); p++)
cout<<*p<<" ";
cout<<endl;

char c;
cin>>c;
return 0;
}

error C2885: “std::vector<_Ty>::iterator”: 在非类范围内不是有效的 using 声明 f:\ch8\ch10\ch10\源2.cpp 7 ch10

cpp(17) : error C2065: “iterator”: 未声明的标识符

为什么会有这个错误提示,而把它改成以下代码的时候就可以通过,不都一样吗????

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


int main()
{
vector<int> container;

for(int i=0; i<=4; i++)
container.push_back(i);

cout<<"Here is what is in the container:\n";
for(vector<int>::iterator p = container.begin(); p != container.end(); p++)
cout<<*p<<" ";
cout<<endl;

cout<<"Setting entries to 0:\n";
for(vector<int>::iterator p = container.begin(); p!=container.end(); p++)
*p=0;

for(vector<int>::iterator p = container.begin(); p != container.end(); p++)
cout<<*p<<" ";
cout<<endl;

char c;
cin>>c;

return 0;

}



关于using std:: 和 using namespace std;的用法想请大家指点下
...全文
625 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2010-01-05
  • 打赏
  • 举报
回复
using namespace std,std里的都可以使用,using std::只使用指定的类型,std其他的类型就不行。
建议用using namespace std;
hlsoven 2010-01-05
  • 打赏
  • 举报
回复
其实原因在于编译器的问题,不同的编译器在处理迭代器的声明,形式是不同的。有的编译器两种形式都支持,我用的编译器声明迭代器时,只支持一种声明方式,也就是第二种形式,所以第一种就不能通过了。
小小攻城师 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hlsoven 的回复:]
引用 1 楼 yshuise 的回复:
using std::vector <int>::iterator;
=============
这只是一个指针:int*

见过:
using std::int* ;这用用法没有??


没有见过。怎么用的啊?
[/Quote]
1楼的意思是说迭代器的本质是用一个指针来实现的
iterator是在vector模板类中实现的,所以要用在类中
这跟using namesapce std;不是同一个概念,这是名字空间
所以他说 vector<int>::iterator这句是类的作用域符号
hlsoven 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yshuise 的回复:]
using std::vector <int>::iterator;
=============
这只是一个指针:int*

见过:
using std::int* ;这用用法没有??
[/Quote]

没有见过。怎么用的啊?
OoPgP 2009-12-30
  • 打赏
  • 举报
回复
你都using掉了,那命名空间的作用呢?
sj13426074890 2009-12-30
  • 打赏
  • 举报
回复
嘻嘻 在VC 6.0下能通过
lori227 2009-12-30
  • 打赏
  • 举报
回复
在头文件里最好不用using namespace std;

如果有另外一个命名空间 using namespace xxx;
如果有两个名字一样的类 就会冲突

一般我都用 std::vector
healer_kx 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 taodm 的回复:]
呃,新手就用using namespace std;吧,别折腾了。

[/Quote]
是啊,我一直用这个。。。
taodm 2009-12-30
  • 打赏
  • 举报
回复
呃,新手就用using namespace std;吧,别折腾了。
yshuise 2009-12-30
  • 打赏
  • 举报
回复
using std::vector <int>::iterator;
=============
这只是一个指针:int*

见过:
using std::int* ;这用用法没有??
skiing886 2009-12-30
  • 打赏
  • 举报
回复
mamu
Contemplating 2009-12-30
  • 打赏
  • 举报
回复
绝大部分情况下,只要用using namespace std;就可以了。

64,651

社区成员

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

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