C++中的类问题
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
class A
{
private:
vector <string> s;
vector <unsigned int> n;
public:
A();
~A(){}
vector <string> A_Name()const{return s;}
vector <unsigned int> A_NN()const{return n;}
friend ostream & operator < <(ostream & os,const A &a);
};
class List
{
private:
string str;
unsigned int money;
public:
List(){}
List(string &Str, unsigned int Money);
List(const List & list);
~List(){}
List & operator+(const List &list);
string Name()const{return str;}
unsigned int Num()const{return money;}
};
A::A():s(),n()
{
//s=vector <string> (m);
//n=vector <unsigned int> (m);
}
ostream & operator < <(ostream & os,const A &a)
{
vector <string> ::iterator it_s=(a.A_Name()).begin();
vector <unsigned int> ::iterator it_unint=(a.A_NN()).begin();
for(;it_s != (a.A_Name()).end(); it_s++, it_unint++)
{
os < < *it_s < < *it_unint < < endl;
}
return os;
}
List::List(string &Str,unsigned int Money)
{
str=Str;
money=Money;
}
List::List(const List &list)
{
str=list.str;
money=list.money;
}
List & List::operator+(const List &list)
{
if(str == list.str)
{
money+=list.money;
}
return *this;
}
int main(void)
{
int n,m;
cin > > n > > m;
vector <A> result;
vector <A> ::iterator it;
while(n != 0 && m != 0)
{
string *st=new string [n];
int *fr=new int [n];
List *LIST=new List[2*m];
List *LIST2=new List[n];
for(int c = 0; c < n; c++)
cin > > st[c];
string str1,str2;
unsigned int money;
for(int c = 0; c < m; c++)
{
cin > > str1 > > str2 > > money;
LIST[2*c]=List(str1,-money);
LIST[2*c+1]=List(str2,money);
}
for(int c = 0; c < n; c++)
{
for(int j = 0; j < 2*m; j++)
{
if(st[c] == LIST[j].Name())
{
fr[c]=j;
break;
}
}
}
for(int c = 0; c < 2*m; c++)
{
for( int j = 0; j < 2*m ; j++)
{
if(j != c)
LIST[c]=LIST[c]+LIST[j];
}
}
for(int c = 0; c < n; c++)
LIST2[c]=List(st[c],LIST[fr[c]].Num());
string st3,st4;
int flag;
for(int c = 0; c < n; c++)
{
if(LIST2[c].Num() < 0)
{
flag =c;
}
//if(LIST2[c].Num() > 0)
//sum++;
}
st4= ' '+LIST2[flag].Name()+ ' ';
A a;
for(int c = 0; c < n; c++)
{
if(LIST2[c].Num() > 0)
{
st3=LIST2[c].Name() +st4;
a.A_Name().push_back(st3);
a.A_NN().push_back(LIST2[c].Num());
}
}
result.push_back(a);
delete [] LIST2;
delete [] LIST;
delete [] fr;
delete [] st;
cin > > n > > m;
}
int c=0;
for(it = result.begin(); it != result.end(); it++,c++)
{
cout < < "Case # " < < c+1 < < endl;
cout < < *it;
}
return 0;
}
此程序能通过编译,不能正常运行。但是我调试了,运行到 A a; 出现问题,提示说好象是堆问题。
[解决办法]
调试下就知道了