vector run wrong !

Discussion in 'C++' started by wukexin, Aug 11, 2003.

  1. wukexin

    wukexin Guest

    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;
    }
    wukexin, Aug 11, 2003
    #1
    1. Advertising

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

    HTH,
    Patrick
    Patrick Frankenberger, Aug 11, 2003
    #2
    1. Advertising

  3. wukexin

    Rolf Magnus Guest

    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);
    > 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;
    > }
    Rolf Magnus, Aug 11, 2003
    #3
  4. "Patrick Frankenberger" <> wrote in message
    news:bh89tp$9j4$01$-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
    John Harrison, Aug 11, 2003
    #4
  5. wukexin

    wukexin Guest

    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" <> дÈëÏûÏ¢ÐÂÎÅ
    :bh8a49$vie0s$-berlin.de...
    >
    > "Patrick Frankenberger" <> wrote in message
    > news:bh89tp$9j4$01$-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
    >
    >
    wukexin, Aug 13, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. learningjava
    Replies:
    5
    Views:
    2,257
    Miguel De Anda
    Oct 17, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,721
  3. Replies:
    8
    Views:
    1,888
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    343
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page