读书人

使用C++的地图出现有关问题

发布时间: 2013-04-09 16:45:09 作者: rapoo

使用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< 写的有问题
[解决办法]

引用:
引用:
引用:引用:
operator< 写的有问题
有没有中文输入法输的“<"?
没见过有人这样用过。我刚试了一下用 《 做函数名是可以的。

其实是这么回事,本来我是没有重载这个运算符的,那时会报编译错误,经过查资料知道要重载这个运算符才行,我用的是英文,一般情……
哦,呵呵。我的8楼是在回答6楼的疑问。你的问题4楼已经回答了。我也在5楼回答了。 重载 < 方法,你写的不对。所以插入数据会出错。
你重载必须保证 a小于b 的同时 b不小于a 才行 你在重载的 < 方法那设置一个断点,看看插入第二个数据时候怎么调用的 operator<,调用了几次,你就明白了
[解决办法]
楼主,买本effecitve stl认真啃啃吧,上面有你这个问题的非常详细的讲解。

读书人网 >C++

热点推荐