使用C++的map出现问题
具体是这样的,我要写一个词法分析器,用到了C++中的map数据结构,在map中插入map<Point ,char> OriginalCharAndPosition;类型的数据,其中Point是我自己定义的类,表示该字符在原
文中的位置。可运行时就出了个断言错,就是在插入数据时出现了问题,是在插入第二行数据时出现的错,该错误是由,Point中重载的<号引起的。具体情况见代码
主函数:
#include "DealCharacter.h"
int main()
{
DealCharacter dealcharacter;
/*map<Point*,char> OriginalCharAndPosition;
vector<int> dealCharPosition;*/
//OriginalCharAndPosition,dealCharPosition
dealcharacter.pro_process();
dealcharacter.coutchar();
return 0;
}
Point.h文件
#ifndef Point_h
#define Point_h
#include <iostream>
using namespace std;
class Point
{
private:
long x,y;
public:
Point(long a=0,long b=0);
Point(const Point &c);
bool operator<(const Point& p)const;
//Point(Point &c);
~Point()
{}
double Getx();
double Gety();
friend istream &operator>>(istream &stream,Point &p);
friend ostream &operator<<(ostream &stream,const Point &p);
};
#endif
Point.cpp文件
#include "Point.h"
Point::Point(long a,long b)
{
x=a;
y=b;
}
//birth::birth(birth &q):year(q.year),month(q.month),day(q.day){}
Point::Point(const Point &c):x(c.x),y(c.y)
{
}
//Point::Point(Point &c)
//{
//x=c.x;
//y=c.y;
//}
double Point::Getx()
{
return x;
}
double Point::Gety()
{
return y;
}
bool Point::operator<(const Point& p)const
{
if(x<p.x)
{
return true;
}else if(y<p.y)
{
return true;
}else{
return false;
}
}
istream& operator>>(istream &stream,Point &p)
{
stream>>p.x >>p.y;
return stream;
}
ostream& operator<<(ostream &stream,const Point &p)
{
stream <<"X:"<<p.x<<" Y:"<<p.y;
return stream;
}
DealCharacter.h文件
#ifndef DealCharacter_h
#define DealCharacter_h
#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include "Point.h"
class DealCharacter{
private:
//用于表示在vector中的当前位置
int i;
map<Point ,char> OriginalCharAndPosition;
vector<int> dealCharPosition;
public:
void pro_process();
void coutchar();
};
#endif
DealCharacter.cpp文件
#include "DealCharacter.h"
//map<Point*,char> OriginalCharAndPosition,vector<int> dealCharPosition
void DealCharacter::pro_process()
{
cout<<"coming to DealCharacter"<<endl;
ifstream cinf("G:/Programming/C++/source.txt",ios::in);
int row=0,col=0;
bool changeRowANDCol=false;
char old_c='\0',cur_c;
//计数器,前一个字符,当前字符。
bool in_comment=false;
//状态标志,false表示当前字符未处于注释中。
while(cinf.read(&cur_c,sizeof(char))){
//从文件读一个字符
changeRowANDCol=false;
switch(in_comment){
case false:
if(old_c=='/' && cur_c=='*')
{
//进入注
dealCharPosition.pop_back();
dealCharPosition.pop_back();
in_comment=true;
}
else {
if(old_c=='\\' && cur_c=='\n')
//去除续行符'\',包括后续换行符。
{
changeRowANDCol=true;
dealCharPosition.pop_back();
dealCharPosition.pop_back();
//去除已存入扫描缓冲区的字符'\'
}
else {
if(cur_c>='A' && cur_c<='Z')
{
cur_c+=32;
}
if(cur_c=='\n')
{
changeRowANDCol=true;
cur_c=' ';//空格
}
if(cur_c=='\t')
{
cur_c=' ';//空格
//buf[i++]=cur_c ;
}
}
dealCharPosition.push_back(row);
dealCharPosition.push_back(col);
}
break;
case true:
if(old_c=='*' && cur_c=='/')
//离开注释
in_comment=false;
}
//end of switch
Point point(row,col);
cout<<"............."<<endl;
cout<<point<<" "<<cur_c<<endl;
cout<<"............."<<endl;
OriginalCharAndPosition.insert(pair<Point, char>(point,cur_c));
if(changeRowANDCol)
{
row++;
col=0;
}else{
col++;
}
old_c= cur_c;
//保留前一个字符
}//end of while
}
void DealCharacter::coutchar()
{
//for(unsigned int i=0;i<dealCharPosition.size();i=i+2)
//{
//if(0==i % 2)
//{
//cout<<"...."<<endl;
//}
//cout<<dealCharPosition[i]<<" "<<dealCharPosition[i+1];
//int j=i+1;
//Point point(i,j);
//map<Point ,char >::iterator it;
//it=OriginalCharAndPosition.find(point);
//if(it!=OriginalCharAndPosition.end())
//{
//cout << "find the elememt" << endl;
//cout<<it->first<<endl;
//cout<<it->second<<endl;
//}else{
//cout << ".........not find the elememt" << endl;
//}
////cout
//
//}
map<Point ,char >::iterator iter;
//see element
for (iter = OriginalCharAndPosition.begin(); iter != OriginalCharAndPosition.end(); iter++ ) {
cout << "| " << iter->first << " | " << iter->
second << " |" << endl;
}
} c++ Map 插入自定义类Point 重载<号出错
[解决办法]
<符号重载逻辑错误
bool Point::operator<(const Point& p)const
{
if(x<p.x)
{
return true;
}else if(x==p.x && y<p.y)
{
return true;
}else{
return false;
}
}
[解决办法]
operator< 写的有问题
[解决办法]