关于->还是.的问题

skineffect 2008-04-05 12:22:43
以下是在C++Primer3上的一个程序的基础上改的:
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
vector<string>* retrieve_text()
{
string file_name;
cout<<"please enter file name:";
cin>>file_name;
ifstream infile(file_name.c_str(),ios::in);

if(! infile)
{
cerr<<"oops!unable to open file"<<file_name<<"--bailing out!"<<endl;
exit(-1);
}
else cout<<endl;

vector<string> *lines_of_text=new vector<string>;
string textline;
typedef pair<string::size_type,int> stats;
//存储最长行的行数和长度。
stats maxline;
int linenum=0;
while(getline(infile,textline,'\n'))
{
cout<<"line read:"<<textline<<endl;
if(maxline.first<textline.size())
{
maxline.first=textline.size();
maxline.second=linenum;
}
lines_of_text->push_back(textline);
linenum++;
}
cout<<"maximum lengh:"<<maxline.first<<endl;
cout<<"the number of the maximum_length line:"<<maxline.second<<endl;
cout<<"number of lines:"<<linenum<<endl;
return lines_of_text;
}
typedef pair<short,short>location;
typedef vector<location>loc;
typedef vector<string>text;
typedef pair<text*,loc*>text_loc;
text_loc* separate_words(const vector<string> *text_file)
{
vector<string> *words=new vector<string>;
vector<location> *locations=new vector<location>;
short line_pos=0;
for(;line_pos<text_file->size();++line_pos)
{
//textline:待处理的当前文本行
//word_pos:文本行中的当前列位置
short word_pos=0;
string textline=(*text_file)[line_pos];
string::size_type pos=0,prev_pos=0;
while((pos=textline.find_first_of(' ',pos))!=string::npos)
{
words->push_back(textline.substr(prev_pos,pos-prev_pos));
//将行/列信息存贮为pair
locations->push_back(make_pair(line_pos,word_pos));
++word_pos;prev_pos=++pos;
}
//处理最后一个单词
words->push_back(textline.substr(prev_pos,pos-prev_pos));
locations->push_back(make_pair(line_pos,word_pos));
}
return new text_loc(words,locations);
}
int main()
{

vector<string> *lines_of_text=new vector<string>;
lines_of_text=retrieve_text();
vector<string>::iterator iter=lines_of_text->begin();
vector<string>::iterator iter_end=lines_of_text->end();
for(;iter!=iter_end;iter++)
cout<<*iter<<endl;
text_loc *text=new text_loc;
text=separate_words(lines_of_text);
iter=text->first->begin();
iter_end=text->first->end();
vector<location>::iterator iter1=text->second->begin();
for(;iter!=iter_end;iter++,iter1++)
{
cout<<*iter<<":"<<(*iter1).first<<","<<(*iter1).second<<endl;//为什么这里是用.,在text_loc中的loc*不是个指针吗,像text->second一样不应该
} //用->吗?(这里换成->程序会报错)

}
...全文
58 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttkk_2007 2008-04-05
  • 打赏
  • 举报
回复
你要搞清楚*iter1里面是什么,是一个location,location是什么,是一个pair<short, short>,而不是pair<short, short> *
所以你当然要用.
而不是->
HengStar 2008-04-05
  • 打赏
  • 举报
回复
for(;iter!=iter_end;iter++,iter1++)
{
cout < <*iter < <":" < <(*iter1).first < <"," < <(*iter1).second < <endl;//为什么这里是用.,在text_loc中的loc*不是个指针吗,像text->second一样不应该
} //用->吗?(这里换成->程序会报错)

vector <location>::iterator iter1=text->second->begin();

这里的iter1的确是个指针类型,它指向一个location对象,而location对象是个pair类型的对象,这里的(*iter1).first表示先对iter1解引用,也就是得到iter1指向的location对象,然后用.操作符取其first成员
icoding 2008-04-05
  • 打赏
  • 举报
回复
iter1应该是一个指针
(*iter1).second
iter1->second
lw1a2 2008-04-05
  • 打赏
  • 举报
回复
(*iter1).second
skineffect 2008-04-05
  • 打赏
  • 举报
回复
明白了,刚才有点乱,散分了。

64,650

社区成员

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

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