CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    vector<string> help needed

    Hello,

    I have a "vector<string> stringVec"and here is what I want to do:

    Read 10 messages from stringVec[0] to stringVec[9]. Then remove these and move stringVec[10] down to stringVec[0] and so forth. Also, if there is less than 10 message just read them all and empty the vector. Can someone help me with sample code to do this?

    Thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: vector<string> help needed

    You'd be better off with a std:eque<std::string> since that has a pop_front() method.

    You can do this with a vector, but it will be less efficient.

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: vector<string> help needed

    Quote Originally Posted by Lindley View Post
    You'd be better off with a std:eque<std::string> since that has a pop_front() method.
    Maybe it's not necessary to actually pop and move stuff. You could try simply std::copyying the data in and out (from the end to the begin of the vector).

    I'm not sure whether the cost of copying std::string would be greater than processing the container itself. I guess it's about trying and experimenting...

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: vector<string> help needed

    Quote Originally Posted by ltcmelo View Post
    You could try simply std::copyying the data in and out (from the end to the begin of the vector).
    I doubt that would be any better than using the vector's erase() member to move stringVec[10] and all the following items down to stringVec[0], implementing what the OP described. (In a single (!) call to erase(), of course.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: vector<string> help needed

    Quote Originally Posted by lab1 View Post
    Read 10 messages from stringVec[0] to stringVec[9]. Then remove these and move stringVec[10] down to stringVec[0] and so forth. Also, if there is less than 10 message just read them all and empty the vector.
    That sounds like a job for std::queue rather than vector.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured