注册 登录
编程论坛 C++教室

[求助]关于namespace的问题

dlcdavid 发布于 2006-12-16 23:22, 1121 次点击

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

int main()
{
int num = 1;//计数
string word;//输入及记录单词
vector<string> vec;//储存文章

while (cin>>word && word[0] != '0')
{
vec.push_back(word);
}

vector<string>::iterator iter1 = vec.begin();
vector<string>::iterator iter2 = vec.begin();
word = *iter2++;

while(iter2 != vec.end())
{
if (*iter1++ == *iter2++)
if (*iter1 == word)
++num;
else
{
int fnum = 2;
while (*iter1 == *iter2 && iter2 != vec.end())
{
++fnum;
++iter1;
++iter2;
}
if(fnum >= num)
{
word = *iter1;
num = fnum;
}
}
}
cout << word << "连续出现了" << num << "次" <<endl;
return 0;
}
--------------------VC++6.0编译-------------------------------------------------
输入:a a a b b b b a a a c c c c c b 0
打印:c连续出现了5次
--------------------------------------------------------------------------------
为什么把
using namespace std;
换成
using std::string;
using std::vector;
using std::iterator;
using std::cin;
using std::cout;
using std::endl;
会出错

6 回复
#2
pusawl2006-12-16 23:36
做个标记,关注这个贴子。
#3
dlcdavid2006-12-17 14:34
?/?????
#4
tancui2006-12-20 11:31
少了一个using std::vector<string>;
#5
tancui2006-12-20 11:34
vec.end()g表示最后一个元素的下一位,你的++可能越界
#6
tancui2006-12-20 11:35
反正我的上面是要出错了,被强行关闭
#7
yuyunliuhen2006-12-20 12:59



这是VC++ 6。0的运行情况:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(23) : error C2653: 'vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<c
har>,class std::allocator<char> > > >' : is not a class or namespace name
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(23) : error C2955: 'iterator' : use of class template requires template argument list
d:\vc98\include\utility(71) : see declaration of 'iterator'
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(23) : error C2514: 'std::iterator' : class has no constructors
d:\vc98\include\utility(71) : see declaration of 'iterator'
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(23) : error C2262: 'iter1' : cannot be destroyed
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(24) : error C2653: 'vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<c
har>,class std::allocator<char> > > >' : is not a class or namespace name
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(24) : error C2955: 'iterator' : use of class template requires template argument list
d:\vc98\include\utility(71) : see declaration of 'iterator'
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(24) : error C2514: 'std::iterator' : class has no constructors
d:\vc98\include\utility(71) : see declaration of 'iterator'
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(24) : error C2262: 'iter2' : cannot be destroyed
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(25) : error C2676: binary '++' : 'struct std::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(27) : error C2676: binary '!=' : 'struct std::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator
C:\Documents and Settings\Administrator\Templates\Cpp1.cpp(27) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

Cpp1.obj - 11 error(s), 0 warning(s)

下面是用C FREE 3.5的运行情况。
正在编译。。。。
正在连接。。。


完成构建::\progra~1\c-free~1.5\temp\untiteled3.cpp 0个错误,0个警告
生成。。。。。。。
这个编译器就没用问题,能够正确的得到结果


楼主说的那个问题 就是用 using 编译声明和编译指令应该是同样的效果才对啊 只是写法不同而已。
我想应该是编译器的问题吧


1