请帮我解决一下这个问题关于顺序容器~~
来源:百度知道 编辑:UC知道 时间:2024/06/21 01:44:09
打算把先读入一个含有char*的list,然后把这个list赋值给一个存储string的vector。用的是c++ builder6,各位看一下哪里出问题了谢谢
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
using namespace std;
int main()
{
list<char*> lst;
vector<string> vec;
char* temp = "";
while(cin>>temp)
{
lst.push_back(temp);
temp = "\0";
}
for(list<char*>::iterator itr = lst.begin();
itr != lst.end();itr++)
{
string mm(*itr);
vec.push_back(mm);
}
for(vector<string
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
using namespace std;
int main()
{
list<char*> lst;
vector<string> vec;
char* temp = "";
while(cin>>temp)
{
lst.push_back(temp);
temp = "\0";
}
for(list<char*>::iterator itr = lst.begin();
itr != lst.end();itr++)
{
string mm(*itr);
vec.push_back(mm);
}
for(vector<string
char* temp = "";
while(cin>>temp)
{
lst.push_back(temp);
temp = "\0";
}
光是这一步就有问题了……你干脆重写程序吧,换成
string temp;
cin>>temp;
char* temp = ""; 这样给temp初始化的时候,temp的大小被默认为1,所以不能给temp赋值。
相应的如果用 char *temp="abc",temp的大小4。
在这里最好使用动态分配给temp设定一个比较适合的大小/。
下面是改了一下的程序。
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
using namespace std;
int main()
{
list<char*> lst;
vector<string> vec;
char* temp = new char[100];//分配空间
while(cin>>temp)
{
lst.push_back(temp);
temp = new char[100];
}
delete[] temp; //回收最后一次分配的空间。
for(list&