vector run wrong !

W

wukexin

I try some kind of compiler, my program compile succeeding, but run wrongly.
Help me, see my program. If you have free time, please give me some
suggestion about process command argument. Thank you very much.
//
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
const bool display_command(vector<string>& s);
bool argument_process(int argc,char* argv[],vector<string>::iterator ptr);
bool display_help();
int main(int argc,char* argv[])
{

static vector<string> message;
vector<string>::iterator ptr=message.begin();
if(argc<1)
{
cerr<<"The program get a abnormal mistake!"<<endl;
}
argument_process(argc,argv,ptr);
return 0;
}
/////////////////////
void print(string s)
{
cout<<s;
}
/////////////////////
const bool display_command(vector<string>& s)
{
for_each(s.begin(),s.end(),print);
return true;
}
////////////////
bool argument_process(int argc,char* argv[],vector<string>::iterator ptr)
{
vector<string> command;
if(1==argc)
{
display_help();
}
for(int i=1;i<argc;i++)
{
command.push_back(argv);
sort(command.begin()+1,command.end());
copy(command.begin()+1,command.end(),ptr);
}
display_command(command);
return true;
}
//
bool display_help()
{
cout<<"display help"<<endl;
return true;
}
 
P

Patrick Frankenberger

wukexin said:
I try some kind of compiler, my program compile succeeding, but run wrongly.
Help me, see my program. If you have free time, please give me some
suggestion about process command argument. Thank you very much.

It doesn't work properly because you are writing past the end of the vector
instead of appending.

Change
vector<string>::iterator ptr=message.begin();
to
vector<string>::iterator ptr=back_inserter(message);
Then you are appending to the vector, as you propably want to.

copy(command.begin()+1,command.end(),ptr);
std::copy assumes std::advance(thirdarg,std::distance(firstarg,secondarg))
to be a valid pointer, which it is not of ptr is begin() of an empty vector.

HTH,
Patrick
 
R

Rolf Magnus

wukexin said:
I try some kind of compiler, my program compile succeeding, but run
wrongly.

What do you mean by "run wrongly"? What happended? What did you expect
to happen instead?
Help me, see my program. If you have free time, please give
me some suggestion about process command argument. Thank you very
much. //
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
const bool display_command(vector<string>& s);
bool argument_process(int argc,char* argv[],vector<string>::iterator
ptr); bool display_help();
int main(int argc,char* argv[])
{

static vector<string> message;
vector<string>::iterator ptr=message.begin();
if(argc<1)
{
cerr<<"The program get a abnormal mistake!"<<endl;
}
argument_process(argc,argv,ptr);
return 0;
}
/////////////////////
void print(string s)
{
cout<<s;
}
/////////////////////
const bool display_command(vector<string>& s)
{
for_each(s.begin(),s.end(),print);
return true;
}
////////////////
bool argument_process(int argc,char* argv[],vector<string>::iterator
ptr)
{
vector<string> command;
if(1==argc)
{
display_help();
}
for(int i=1;i<argc;i++)
{
command.push_back(argv);
sort(command.begin()+1,command.end());
copy(command.begin()+1,command.end(),ptr);


Here is probably your problem. The vector you created in main() has a
size of 0. Now you try to copy the contents of the vector 'command'
into non-existing space. You must use a back_insterter instead or
resize your vector before copying, but for both, you'd need the vector
not just an iterator.
 
J

John Harrison

Patrick Frankenberger said:
It doesn't work properly because you are writing past the end of the vector
instead of appending.

Change
to
vector<string>::iterator ptr=back_inserter(message);
Then you are appending to the vector, as you propably want to.

Right idea, but back_inserter does not return a vector<string>::iterator
therefore your code won't compile.

john
 
W

wukexin

Thank you very much, you make me see me ignorance in some key detail. Thank
you very much again. My program run succeeding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top