如何优化C++程序的内存,此程序貌似在T=100的时候需要很大的空间

ladiesman 发布于 2013/04/06 12:35
阅读 143
收藏 0

多人传话系统,传话产生误差.

//source code

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
char cut(char c)
{
 if (::isalpha(c) || '\0'==c)
  return c;
 else
  return ' ';
}
int main()
{
int T,M,N;
cin>>T;

vector<string> *vect=new vector<string>[T];
for (int numofGroup = 0; numofGroup < T; numofGroup++)
{
cin>>N>>M;
string sentence;
string *origWord=new string[M];
string *finalWord=new string[M];
for (int i = 0; i < M; i++)
{
cin>>*(origWord+i)>>*(finalWord+i);
}
fflush(stdin);
getline(cin,sentence);
transform(sentence.begin(),sentence.end(),sentence.begin(),cut);
unsigned int locbeg=sentence.find(' ',0);


if (locbeg!=0)
{
vect[numofGroup].push_back(string(sentence,0,locbeg));
}
while(1)
{
unsigned int locend=sentence.find(' ',locbeg+1);
if (locbeg+1==locend)
{
continue;
}
else if (locend==string::npos )    
        {
     if (locbeg!=sentence.size()-1)      
 vect[numofGroup].push_back(string(sentence, locbeg+1, sentence.size()-locbeg-1));
 break;
        }
else
{
vect[numofGroup].push_back(string(sentence,locbeg+1,locend-locbeg-1));
}
locbeg=locend;
}
for (vector<string>::size_type t=0;t<vect[numofGroup].size();t++)
{
for (int i=0;i<M;i++)
{
if (vect[numofGroup][t]==origWord[i])
{
vect[numofGroup][t]=finalWord[i];
for (int test = i+1; test <M ; test++)
{
if (vect[numofGroup][t]==origWord[test])
{
vect[numofGroup][t]==finalWord[test];
}
}
break;
}
}
}
}

for (int i=0;i<T;i++)
{
cout<<"Case #"<<i+1<<":";
for (vector<string>::size_type t=0;t<vect[i].size();t++)
{
cout<<vect[i][t]<<' ';
}
cout<<endl;
}
}
加载中
0
i
ioll
vector<vector<unique_ptr<string> > >
返回顶部
顶部