关于使用using std::cin;与使用using namespace std;的问题??

jiayouwyhit 2013-08-19 10:16:25
各位大侠,小弟碰到一个老生长谈的问题,是这样的,本人谢了段如下代码,用到了set容器,在主函数外面如果用using namespace std;则一下代码能够顺利通过并运行,但是如代码所述,使用
using std::cin;
using std::cout;
using std::endl;
则会报出一大堆的错误,主要是说找不到set,string等等头文件,错误显示如:
C:\Documents and Settings\Administrator\桌面\MYOWNTESTPROGRAM\MapTest\MapTest.cpp(15) : error C2065: 'set' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\MYOWNTESTPROGRAM\MapTest\MapTest.cpp(15) : error C2065: 'string' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\MYOWNTESTPROGRAM\MapTest\MapTest.cpp(15) : error C2065: 'excluded' : undeclared identifier
可是我明明添加了头文件的啊,小弟很纠结,这个具体原因是为什么呢?

代码如下:
#include "stdafx.h"
#include <set>
#include <string>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;


int main(int argc, char* argv[])
{
set<string> excluded;

string readedBooks;
while(cin>>readedBooks && readedBooks!="q")
excluded.insert(readedBooks);
cout<<"The input is:"<<endl;
for (set<string>::iterator iter=excluded.begin();iter!=excluded.end();++iter)
{
cout<<*iter<<endl;
}
return 0;
}
...全文
300 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiayouwyhit 2013-08-19
  • 打赏
  • 举报
回复
谢谢各位,分少了点,没法都给你们啊。
jiayouwyhit 2013-08-19
  • 打赏
  • 举报
回复
我大概知道了,因为set,string,iostream等也是定义在namespace std里面,可是为啥添加了头文件了,还需要加一个using namespace std呢?
你怎么了熊吉 2013-08-19
  • 打赏
  • 举报
回复
你用到了什么就得using什么 using std::set using std::string 而using namespace std相当于一次性把std里的东西全using了
帅得不敢出门 2013-08-19
  • 打赏
  • 举报
回复
std的作用域包括了 cin cout set string
大尾巴猫 2013-08-19
  • 打赏
  • 举报
回复
1、using namespace std 2、using std::string using std::set ... 3、定义对象的时候加std:: ,如 std::string str 以上3个方法任选其一。
FancyMouse 2013-08-19
  • 打赏
  • 举报
回复
std::set std::string

64,686

社区成员

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

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