关于vector的

cricket_988 2004-11-21 11:50:09
程序如下:
#include <iostream>
#include <iomanip>
#include <ios>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::streamsize;
int main()
{
cout<<"please input a sentence: ";
vector<string> sentence;
string x;
while(cin>>x && x!="." )
sentence.push_back(x);
//int num=sentence.size();


typedef vector<string>::size_type vec_sz;
vec_sz size=sentence.size();
int num=size;

cout<<num<<endl;
return 0;
}
老是报错:
Cpp2.cpp
G:\TurboCpp\cppex\cpp2\Cpp2.cpp(22) : 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<char>,class std::allocator<c
har> > > >' : is not a class or namespace name
G:\TurboCpp\cppex\cpp2\Cpp2.cpp(22) : error C2146: syntax error : missing ';' before identifier 'vec_sz'
G:\TurboCpp\cppex\cpp2\Cpp2.cpp(22) : error C2065: 'vec_sz' : undeclared identifier
G:\TurboCpp\cppex\cpp2\Cpp2.cpp(23) : error C2146: syntax error : missing ';' before identifier 'size'
G:\TurboCpp\cppex\cpp2\Cpp2.cpp(23) : error C2065: 'size' : undeclared identifier
Error executing cl.exe.

Cpp2.exe - 5 error(s), 0 warning(s)
不知道以下三句有什么不对
typedef vector<string>::size_type vec_sz;
vec_sz size=sentence.size();
int num=size;
若换成
//int num=sentence.size();
就能成功
谁知道是怎么回事?还有就是用这三句有什么意义?
刚学c++,希望讲的具体点
...全文
89 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cricket_988 2004-11-22
  • 打赏
  • 举报
回复
试过了,可以,多谢了,那这样写和直接写int num=sentence.size();效果一样,两者有什么不同吗?
pacman2000 2004-11-22
  • 打赏
  • 举报
回复
编译器的问题吧? 程序用g++2.95编译过了,除了不需要包含<ios>这个头文件。
张志龙 2004-11-22
  • 打赏
  • 举报
回复
using namespace std;
很有用!
carylin 2004-11-22
  • 打赏
  • 举报
回复
一般在前面加上语句
using namespace std;
避免全局名字污染问题。
greenteanet 2004-11-22
  • 打赏
  • 举报
回复
我觉得应该是一样的,你错误的地方只是少了一个std::,但是这就足于造成很大的错误啦。其实我觉得可以在程序头写上using namespace std;这样子就不用加std::了。
xiaolizi 2004-11-21
  • 打赏
  • 举报
回复
typedef vector<string>::size_type vec_sz;
改成 typedef std::vector<string>::size_type vec_sz;
或者在开头:
using std::vector<string>::size_type;

一般直接在包含头文件后using namespace std;就可以了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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