By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

vector run wrong !

P: n/a
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[i]);
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;
}
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"wukexin" wrote:
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,secon darg))
to be a valid pointer, which it is not of ptr is begin() of an empty vector.

HTH,
Patrick
Jul 19 '05 #2

P: n/a

"Patrick Frankenberger" <p.*************@gmx.net> wrote in message
news:bh*************@news.t-online.com...

"wukexin" wrote:
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.


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

john
Jul 19 '05 #3

P: n/a
wukexin wrote:
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[i]);
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.

}
display_command(command);
return true;
}
//
bool display_help()
{
cout<<"display help"<<endl;
return true;
}


Jul 19 '05 #4

P: n/a
Thank you very much, you make me see me ignorance in some key detail. Thank
you very much again. My program run succeeding.
"John Harrison" <jo*************@hotmail.com> 写入消息新闻
:bh************@ID-196037.news.uni-berlin.de...

"Patrick Frankenberger" <p.*************@gmx.net> wrote in message
news:bh*************@news.t-online.com...

"wukexin" wrote:
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.


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

john

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.