求助:如何将string中字符装进vector容器中?

jzzlee 2012-04-20 02:25:29
s中存着一串字符串,想把它每个字母拆出来,加上它出现的次数,构成一个结点node,放进vec容器,可以通过编译,但是无法运行。麻烦各位帮忙看下,十分感谢!
struct node
{
string ch;
int weight;
node():ch('\0'),weight(0){}
};


void change(string s,vector<node> &vec)
{
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
node temp;temp.ch=s[i];temp.weight=1;
for(j=vec.begin();j!=vec.end();)
{
if(j->ch==temp.ch)
{
temp.weight+=j->weight;j=vec.erase(j);vec.insert(j,temp);
}
else
{
++j;
if(j==vec.end()) vec.push_back(temp);
}

}
}
for(vector<node>::size_type i=0;i!=vec.size();++i)
vec[i].weight=100*vec[i].weight/s.size();
}
...全文
791 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jzzlee 2012-04-21
  • 打赏
  • 举报
回复
嘿嘿,不好意思了,谢谢哈,CSDN这么多人来,就是因为你们能够无私的帮助别人。在你们的帮助下,到今晚,历经近一天的努力,我的程序终于写好了。输入文件名,从文件中读取字符,出现的每种字符和它出现的概率作为一个结点,将结点依次放入进容器,构建哈夫曼树,生成哈弗曼编码,输入一串数字,翻译出对应的字符,创建string.txt文件,将翻译的结果存入该文件。

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
typedef char **huffmancode;
struct node
{
char ch;
int weight;
node():ch('\0'),weight(0){}
};
typedef struct htnode
{
node data;
unsigned parent,left,right;
htnode():data(),parent(0),left(0),right(0){}
}*huffman;
int file_string(string filename,string& svec)
{
//________________________________________读文件内容
ifstream infile(filename.c_str());
if(!infile)
{
return 1;
}
string s;
while(getline(infile,s))
svec=s;
infile.close();
if(infile.eof())
return 4;
if(infile.bad())
return 2;
if(infile.fail())
return 3;
}
void change(string s,vector<node>& vec)
{
//___________________________________________将字符装进vec容器,并统计概率
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
node temp;temp.ch=s[i];temp.weight=1;
for(j=vec.begin();j!=vec.end();j++)
{
if(j->ch==temp.ch)
{
j->weight++;
break;
}
}
if(j==vec.end())
vec.push_back(temp);
}
for(vector<node>::size_type i=0;i!=vec.size();++i)
vec[i].weight=100*vec[i].weight/s.size();
}
int min(htnode *ht,int i)
{
int j,flag;unsigned k=60000;
for(j=1;j<=i;++j)
if(ht[j].data.weight<k&&ht[j].parent==0)
k=ht[j].data.weight,flag=j;
ht[flag].parent=1;
return flag;
}
void select(htnode *ht,int i,int &s1,int &s2)
{
//_________________________________________选择两个权值最小项
int j;
s1=min(ht,i);
s2=min(ht,i);
if(s1<s2)
{
j=s1;
s1=s2;
s2=j;
}
}
void huffmantree(huffman &HT,huffmancode &HC,vector<node> vec,int n)
{
//_________________________________________________构造赫夫曼树HT
int m,i,s1,s2,start;
unsigned c,f;
huffman p;
char *cd;
if(n<=1)
return;
m=2*n-1;
vector<node>::iterator it;
HT=(huffman)malloc((m+1)*sizeof(htnode));
for(p=HT+1,i=1,it=vec.begin();i<=n;++i,++p,++it)
{
p->data=*it;
p->parent=0;
p->left=0;
p->right=0;
}
for(;i<=m;++i,++p)
p->parent=0;
for(i=n+1;i<=m;++i)
{
select(HT,i-1,s1,s2);
HT[s1].parent=HT[s2].parent=i;
HT[i].left=s1;
HT[i].right=s2;
HT[i].data.weight=HT[s1].data.weight+HT[s2].data.weight;
}
//___________________________________________________________从叶子到根求每个字符的赫夫曼编码
HC=(huffmancode)malloc((n+1)*sizeof(char*));

cd=(char*)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].left==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}

int main()
{
//_____________________________________________________________主函数
string filename,str;
cout<<"输入文件名:"<<endl;
cin>>filename;
switch(file_string(filename,str))
{
case 1:
cout<<"打开失败"<<filename<<endl;
return -1;
case 2:
cout<<"系统错误"<<endl;
return -1;
case 3:
cout<<"读文件错误!"<<endl;
return -1;
}
cout<<str<<endl;
vector<node> vec;
change(str,vec);
for(vector<node>::size_type i=0;i!=vec.size();++i)
cout<<vec[i].ch<<" "<<vec[i].weight<<endl;
//______________________________________________________
int n=vec.size();
cout<<n<<endl;
huffman ht;huffmancode hc;
huffmantree(ht,hc,vec,n);
for(int i=1;i<=n;i++)
puts(hc[i]);
//_______________________________________________
string ss;
cout<<"请输入编码:"<<endl;
cin>>ss;
string d;
for(string::size_type mm=0;mm!=ss.size();)
{
int parent=2*n-1;
while(ht[parent].left!=0)
{
if(ss[mm]=='0')
parent=ht[parent].left;
else
parent=ht[parent].right;
++mm;
}
d=d+ht[parent].data.ch;
}
cout<<d<<endl;
//_________________________________创建string.txt文件,向其中存入d的内容
ofstream SaveFile("string.txt");
SaveFile<<d;
return 0;
}

[Quote=引用 8 楼 的回复:]

引用 7 楼 的回复:
谢谢哥们了,一中午半下午没结果,,,终于搞定了,

引用 6 楼 的回复:

change函数那里错了,如果vec为空根本进不去,另外没看懂你为啥*100/size,去掉了
C/C++ code
void change(string s,vector<node>&amp;amp; vec)
{
vector<node>::iterator j;……
[/Quote]
evencoming 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
谢谢哥们了,一中午半下午没结果,,,终于搞定了,

引用 6 楼 的回复:

change函数那里错了,如果vec为空根本进不去,另外没看懂你为啥*100/size,去掉了
C/C++ code
void change(string s,vector<node>&amp; vec)
{
vector<node>::iterator j;
for(string::size_……
[/Quote]
我辛苦改好的被无视了。。
jzzlee 2012-04-20
  • 打赏
  • 举报
回复
谢谢哥们了,一中午半下午没结果,,,终于搞定了,
[Quote=引用 6 楼 的回复:]

change函数那里错了,如果vec为空根本进不去,另外没看懂你为啥*100/size,去掉了
C/C++ code
void change(string s,vector<node>& vec)
{
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
n……
[/Quote]
nice_cxf 2012-04-20
  • 打赏
  • 举报
回复
change函数那里错了,如果vec为空根本进不去,另外没看懂你为啥*100/size,去掉了
void change(string s,vector<node>& vec)
{
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
node temp;temp.ch=s[i];temp.weight=1;
for(j=vec.begin();j!=vec.end();j++)
{
if(j->ch==temp.ch)
{
j->weight++;
break;
}
}
if(j==vec.end())
vec.push_back(temp);
}
}
jzzlee 2012-04-20
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct node
{
char ch;
int weight;
node():ch('\0'),weight(0){}
};
struct htnode
{
node data;
unsigned parent,left,right;
htnode():data(),parent(0),left(0),right(0){}
};
int file_string(string filename,string& svec)
{
ifstream infile(filename.c_str());
if(!infile)
{
return 1;
}
string s;
while(getline(infile,s))
svec=s;
infile.close();
if(infile.eof())
return 4;
if(infile.bad())
return 2;
if(infile.fail())
return 3;
}
void change(string s,vector<node>& vec)
{
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
node temp;temp.ch=s[i];temp.weight=1;
for(j=vec.begin();j!=vec.end();)
{
if(j->ch==temp.ch)
{
j->weight++;
break;
}
else
{
++j;
}
if(j==vec.end())
vec.push_back(temp);
}
}
for(vector<node>::size_type i=0;i!=vec.size();++i)
vec[i].weight=100*vec[i].weight/s.size();
}

int main()
{
string filename,str;
cout<<"输入文件名:"<<endl;
cin>>filename;
switch(file_string(filename,str))
{
case 1:
cout<<"打开失败"<<filename<<endl;
return -1;
case 2:
cout<<"系统错误"<<endl;
return -1;
case 3:
cout<<"读文件错误!"<<endl;
return -1;
}
cout<<str<<endl;
vector<node> vec;
change(str,vec);
for(vector<node>::size_type i=0;i!=vec.size();++i)
cout<<vec[i].ch<<" "<<vec[i].weight<<endl;
//______________________________________________________
return 0;
}

这样怎么只能输出字符串,不能输出容器里的内容啊?
jzzlee 2012-04-20
  • 打赏
  • 举报
回复
if(j==vec.end()) 
vec.push_back(temp);

不是这个吗?
[Quote=引用 3 楼 的回复:]

结构里面的ch类型应该是signed char verctor的赋值部分不知道在那里也要做对应修改
[/Quote]
nice_cxf 2012-04-20
  • 打赏
  • 举报
回复
结构里面的ch类型应该是signed char verctor的赋值部分不知道在那里也要做对应修改
evencoming 2012-04-20
  • 打赏
  • 举报
回复
node里面的第一个成员应该是char而不是string,改为
[code=C/C++]
struct node
{
char ch;
int weight;
node():ch('\0'),weight(0){}
};
void change(string s,vector<node> &vec)
{
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
node temp;temp.ch=s[i];temp.weight=1;
for(j=vec.begin();j!=vec.end();)
{
if(j->ch==temp.ch)
{
j->weight+=1;
break;
}
else
{
++j;
}
if(j==vec.end())
vec.push_back(temp);
}
}
for(vector<node>::size_type i=0;i!=vec.size();++i)
vec[i].weight=100*vec[i].weight/s.size();
}
[/CODE]
jzzlee 2012-04-20
  • 打赏
  • 举报
回复

struct node
{
string ch;
int weight;
node():ch('\0'),weight(0){}
};
void change(string s,vector<node> &vec)
{
vector<node>::iterator j;
for(string::size_type i=0;i!=s.size();++i)
{
node temp;temp.ch=s[i];temp.weight=1;
for(j=vec.begin();j!=vec.end();)
{
if(j->ch==temp.ch)
{
temp.weight+=j->weight;j=vec.erase(j);vec.insert(j,temp);
}
else
{
++j;
if(j==vec.end()) vec.push_back(temp);
}

}
}
for(vector<node>::size_type i=0;i!=vec.size();++i)
vec[i].weight=100*vec[i].weight/s.size();
}

64,691

社区成员

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

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